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

How do I trigger a function as long as the player holds down the spacebar?

I need to play an animation, resize a mesh, and activate a bodyForce object for as long as the player holds down the spacebar, and then turn them off and resize the mesh again as soon as they lift it up.

How would I check for that?

This was my first thought:

k = {}

mouse.KeyDown:connect(function(b) k[b] = true end)
mouse.KeyUp :connect(function(b) k[b] = nil end)

while wait() do
if k[Enum.KeyCode.Space] then
--blahghblah
end


if k[Enum.KeyCode.Space]==nil then
--blaghblah
end

But, well, it doesn't work, and I don't really want to use a loop for this.

2 comments

0

Well first, don't use KeyDown/KeyUp because it's deprecated. Also, you're not checking if it's the Spacebar that's being pressed. Also, on line 4, you should be setting k[b] to false, not nil. You also don't need a loop. 

0

'nil' also works too...

2 answers

Try this?

function onPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Space then
		print("Space was pressed")
	end
end

function OnRelease(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.Space then
		print("Space was released")
	end
end

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:connect(onPress)
UserInputService.InputEnded:connect(OnRelease)

3 comments

0

Nope, that doesn't do anything either.

0

Make sure it's in a LocalScript inside the client (StarterGui?)

0

:T It didn't work when I put it into starterpack, but it does in startergui. Well, thanks.

Using a boolean upvalue and a repeat ... until or while ... end loop, one could do this:



local userInputService = game:GetService("UserInputService")

local enabled


userInputService.InputBegan:connect(function(input, processed)
	-- If Roblox hasn't already processed this input;
	if not processed then
		-- If the key pressed is the space bar;
		if input.KeyCode == Enum.KeyCode.Space then
			print("Space key down")
			
			-- Reset 'enabled' to true.
			enabled = true
			
			-- Repeat this code until our 'InputEnded' callback sets
			-- 'enabled' to false.
			repeat
				print("Space key held")
				wait()
			until not enabled
		end
	end
end)

userInputService.InputEnded:connect(function(input, processed)
	-- If Roblox hasn't already processed this input;
	if not processed then
		-- If the key that is no longer being pressed is the space bar;
		if input.KeyCode == Enum.KeyCode.Space then
			print("Space key up")
			
			-- Set enabled to false, forcing the 'repeat ... until' loop
			-- to break.
			enabled = false
		end
	end
end)


The code should be pretty explanatory, but if you need further explanations, feel free to leave a comment. And please, if this helped, make sure to mark my answer as accepted and optionally upvote. It helps both of us.

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