2D Player Movement

Hey! Thanks for checking this post. (:

Basically I want to move the player Upwards if the ‘W’ button is being held down. The code I currently have works, but I have to spam ‘W’. I’ve looked on the wiki and I just cant seem to find anything that worked. Thanks!

My Code:

local UIS = game:GetService("UserInputService")


UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		local plr = game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('ScreenGui'):WaitForChild('Player')
		plr.Position = UDim2.new(.5, 0, plr.Position.Y.Scale + -0.01, 0 )
	end
end)

I’ve Tried :IsKeyDown() But it also doesn’t seem to work with my code. (Not sure if I’m doing it right either so heres the :KeyDown() Code.

KeyDown:

local UIS = game:GetService("UserInputService")

local wHeld = UIS:IsKeyDown(Enum.KeyCode.W)

function xd()
	if wHeld then
		local plr = game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('ScreenGui'):WaitForChild('Player')
		plr.Position = UDim2.new(.5, 0, plr.Position.Y.Scale + -0.01, 0 )
	end
end

UIS.InputBegan:Connect(xd)
1 Like

The issue here is you’re only running the code when the InputBegan event is fired. This event only runs when a key is pressed, but does not continue running if the key is being held.

Probably the best way to do this is create a while loop in which you check if the key is being held and then move the player accordingly.

1 Like

Can you give an example on how I would do this? I’m not quite sure exactly what you’re stating and which event to fire.

Edit:

This didnt work, not sure if this is exactly what you’re trying to get me to do here.

local UIS = game:GetService("UserInputService")

local wHeld = UIS:IsKeyDown(Enum.KeyCode.W)

while wait() do
	if wHeld then
		local plr = game.Players.LocalPlayer:WaitForChild('PlayerGui'):WaitForChild('ScreenGui'):WaitForChild('Player')
		plr.Position = UDim2.new(.5, 0, plr.Position.Y.Scale + -0.01, 0 )
	end
end

You don’t need to use any events for this. You need to continually check if a key is being pressed and the best way to do so is using a while loop. Here’s an example:

while wait() do
    if UIS:IsKeyDown(Enum.KeyCode.W) then -- checks if the w key is currently pressed down
          -- move player code here
    end
end
3 Likes

Thank you! Works phenomenal! :smiley_cat: Have a great day! :smiley:

Yuup, no problem!

I just wanted to point out, the reason why it’s not working here is because you checked if the key was down only one time and assigned it to a value ‘wHeld’. You should be checking every single time the loop repeats itself, like in my example.

1 Like

Typically you’d want to have your evaluation between the while do loop. I find that relying on the fact that wait returns a value to use in a loop is strange, unless you attached it to an RBXScriptSignal.

while UIS:IsKeyDown(Enum.KeyCode.W) do
    -- code
    wait() -- Or wherever you need it
end

-- or

while true do
    if UIS:IsKeyDown(Enum.KeyCode.W) then
        -- code
    end
    wait()
end

Someone wrote a lengthy article about the While Wait Do idiom, but I can’t recall where exactly I left it.

1 Like

Possibly?

No, not that. Just found it.

I went to go find it since I was interested in reading it and since I was prompted by this post, looks like I lucked out. It’s a Google Document written by @cntkillme, titled While-Wait-Do Idiom.

I copied the link from mobile so let me know if it doesn’t work and I’ll try to get a proper one when I get on a PC.

I can’t remember how I attained it, sadly. I’m pretty sure he wrote other documents about certain practices by Roblox developers, but I don’t have them.

Will post to resources later, maybe.

4 Likes

Hmm, interesting document! Thanks for sharing it. :slight_smile:

I’d like to point out another reason why I (and maybe others as well) sometimes put wait() between while and do is because it sorta adds a feeling of safety. Just doing while true do seems a bit scary because if I forget to put some sort of delay, it’ll crash studio during testing and potentially lose some of my work (which used to happen to me often). But I do see why its a bad programming practice.

2 Likes