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

Holding key issue help?

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

✓ Accepted answer

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)

5 comments

0

You need to explain what you've done. Just posting some code wouldn't help anyone learn..

0

Well I understood completely.

0

It works! :D

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.

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

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