Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

KeyDown not working as expected?

I've just started playing around with moving a part with w a s d - It works, to some extent. The part moves as it should but when 'w' is released it still keeps going. There is probably a really simple solution that I can't figure out... But eh

local plr = game.Players.LocalPlayer
local ctrl = game.Workspace.Focus
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "w" then
		while key == "w" do
			ctrl.Position = ctrl.Position + Vector3.new(0,0,1)
			wait()
				
			
		end
	end
end)

(ctrl is the part I'm trying to move) Thank you for any help :)

1 answer

✓ Accepted answer

Adding onto what yumtaste said, you need to detect when the key is released and then update the loop, but he didn't do that correctly

local moving = false
mouse.KeyDown:connect(function(key)
    if key == "w" then
        moving = true
        while moving do
                ctrl.Position = ctrl.Position + Vector3.new(0,0,1)
                wait()
        end
    end
end)
mouse.KeyUp:connect(function(key)
        if key == "w" then
                moving = false
        end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.