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

Holding key issue help?

Asked by
Hero_ic 502 Moderation Voter
9 years ago

I'm having a problem when

if key == "d" then
    dir = dir + 1
end

this function happens i want it to hold not only tap it once and i'm having trouble with that basically what i'm trying to do is make a loop when its pressed and when its released the loop ends.

1 answer

Log in to vote
7
Answered by 9 years ago

If your going to use Keydown() then I recommend you use a LocalScript since it a lot easier to access the Player/Character using local script.

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()

TurnD = false
Mouse.KeyDown:connect(function(key)-- Parameter
    if key:lower()=="f" then--This checks if the lowercase letter "f" has been clicked" if it has it continues the script
   TurnD = not TurnD-- Changes the TurnD to false and true.It works like this: It check value/boolean if its false it turns it true if its true it turns it false.
print(TurnD)
while TurnD == true do--This checks if TurnD is true if it is then it starts the while true loop.
--Code
wait(1)  
if TurnD == false then-- If TurnD is false then it breaks the loop
break
end
        end
    end
end)
0
You need to explain what you've done. Just posting some code wouldn't help anyone learn.. Uroxus 350 — 9y
0
Well I understood completely. Hero_ic 502 — 9y
0
It works! :D Hero_ic 502 — 9y
1
@MrLegoman900 I am but I decide to most it before explain it so people can see I'm answering the question so they don't have to. Then I edit it and explain how it works. UserOnly20Characters 890 — 9y
0
Mouse.KeyDown was deprecated and the description says "This member exists for compatibility and should not be used in new scripts.", you may want to consider using UserInputService instead to check for key events VariadicFunction 335 — 9y
Ad

Answer this question