Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

KeyDown not working as expected?

Asked by
PredNova 130
8 years ago

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

Log in to vote
2
Answered by 8 years ago

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
Ad

Answer this question