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

How would I make a function run as long as a key is held?

This is what I have for Userinput. How would I make it run "Walking" for as long as the key is held?

    mouse.KeyDown:connect(function(key)
        if key == "w" or key == "a" or key == "s" or key == "d" and script.Parent.Walking.Value==false then
	walking()
	end
end)

1 answer

There's many ways, but here's the simplest way:

local keysDown = {}

mouse.KeyDown:connect(function(key)
	keysDown[key:lower()] = true
end)

mouse.KeyUp:connect(function(key)
	keysDown[key:lower()] = false
end)

while true do
	if (keysDown["w"] or keysDown["a"] or keysDown["s"] or keysDown["d"]) and not script.Parent.Walking.Value then
		walking()
	end
	wait()
end

EDIT: key:lower() gets the lowercase of the key, just in case.

2 comments

0

What does key:lower do? Can you explain your answer a bit? I would like to learn how it functions. Thanks

0

Nvm, I get it.

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