Okay so I have a Localscript found in the player's screengui in startergui. It's basic function is simple. Move a image frame when you press a letter. Here is my problem, when the user holds down a key I want the player to keep moving until they let go of the key, how exactly would I do this?
The code:
repeat wait() until game.Players.LocalPlayer.PlayerGui ---------------------------------------------------------- -- Disable roblox's dumb stuff ---------------------------------------------------------- game.StarterGui:SetCoreGuiEnabled("All", false) ---------------------------------------------------------- -- Global Variables ---------------------------------------------------------- local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local sprite = script.Parent.Player ---------------------------------------------------------- -- Movement ---------------------------------------------------------- function moveLeft() sprite.Position = sprite.Position - UDim2.new(0.01,0,0,0) end function moveRight() sprite.Position = sprite.Position + UDim2.new(0.01,0,0,0) end ---------------------------------------------------------- -- Keyboard input ---------------------------------------------------------- Mouse.KeyDown:connect(function(Key) if Key == "a" then moveLeft() end if Key == "d" then moveRight() end end)
Here's how I would write this code:
repeat wait() until game.Players.LocalPlayer.PlayerGui ---------------------------------------------------------- -- Disable roblox's dumb stuff ---------------------------------------------------------- game.StarterGui:SetCoreGuiEnabled("All", false) ---------------------------------------------------------- -- Global Variables ---------------------------------------------------------- local Player = game.Players.LocalPlayer local Mouse = Player:GetMouse() local sprite = script.Parent.Player local keysDown = {} ---------------------------------------------------------- -- Movement ---------------------------------------------------------- game:GetService("RunService").RenderStepped:connect(function() local move = 0 if keysDown.a then move = move - 1 end if keysDown.d then move = move + 1 end sprite.Position = sprite.Position + UDim2.new(0.01*move,0,0,0) end) ---------------------------------------------------------- -- Keyboard input ---------------------------------------------------------- Mouse.KeyDown:connect(function(Key) keysDown[Key] = true end) Mouse.KeyUp:connect(function(Key) keysDown[Key] = false end)
This is a variant of what Perci1 suggested you do.
I'd use a 'while' loop and some static boolean values to make sure that it's doing what it should be doing at a given time.
isGoingLeft = false isGoingRight = false Mouse.KeyDown:connect(function (Key) if Key:lower() == "a" then isGoingLeft = true while isGoingLeft do if isGoingLeft == false then break end moveLeft() if isGoingLeft == true then wait(.25) elseif isGoingLeft == false then break end end end if Key:lower() == "d" then isGoingRight = true while isGoingRight do if isGoingRight == false then break end moveRight() if isGoingRight == true then wait(.25) elseif isGoingRight == false then break end end end end Mouse.KeyUp:connect(Key) if Key:lower() == "a" then isGoingLeft = false end if Key:lower() == "d" then isGoingRight = false end