RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a script that allows an entity to move through a 3 x 6 grid ahen the arrow keys are pressed"
-- local script local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local humanoidRootPart = character:WaitForChild("HumanoidRootPart") local gridSize = Vector3.new(3, 0, 6) local moveSpeed = 0.5 local function move(direction) local newPosition = humanoidRootPart.Position + (gridSize * direction) humanoidRootPart:MoveTo(newPosition) end local function onKeyPress(actionName, userInputState, inputObject) if userInputState == Enum.UserInputState.Begin then if actionName == "MoveUp" then move(Vector3.new(0, 0, -1)) elseif actionName == "MoveDown" then move(Vector3.new(0, 0, 1)) elseif actionName == "MoveLeft" then move(Vector3.new(-1, 0, 0)) elseif actionName == "MoveRight" then move(Vector3.new(1, 0, 0)) end end end local contextActionService = game:GetService("ContextActionService") contextActionService:BindAction("MoveUp", onKeyPress, false, Enum.KeyCode.W, Enum.KeyCode.Up) contextActionService:BindAction("MoveDown", onKeyPress, false, Enum.KeyCode.S, Enum.KeyCode.Down) contextActionService:BindAction("MoveLeft", onKeyPress, false, Enum.KeyCode.A, Enum.KeyCode.Left) contextActionService:BindAction("MoveRight", onKeyPress, false, Enum.KeyCode.D, Enum.KeyCode.Right)