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

Why when I press button, it's only running once? (UserInputServices)

everything working fine exept whenever I press E, it only runs once and if I press E again it doesn't move to a random location again.

UIS = game:GetService("UserInputService")
Trans = game.Workspace.Transform
local posi = Vector3.new(math.random(1,50), math.random(1,30), math.random(1,40))
local held = false	

UIS.InputBegan:Connect(function(input, GameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.E
	then
		wait()
		Trans.Transparency = 0.5
		Trans.Position = posi
		print ("pressed down E key")
		held = true
		
		while held == true
		do
			wait()
			print("holding the key")
		end
	end
end)

UIS.InputEnded:Connect(function(input, GameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.E
	then
		held = false
	end
end)

2 comments

0

Just add a while loop because the server constantly checks wether the player is pressing e

0

Add line 1

1 answer

EDIT: fixed, i put my "posi" variable on the outside and for some reason it doesn't work that way so I put it inside the if statement

UIS = game:GetService("UserInputService")
Trans = game.Workspace.Transform
local held = false	




 UIS.InputBegan:Connect(function(input, GameProccessedEvent)

	if input.KeyCode == Enum.KeyCode.E
	then
		wait()
		Trans.Transparency = 0.5
		Trans.Position = Vector3.new(math.random(1,100), math.random(1,100), math.random(1,100))
		print ("pressed down E key")
		held = true
		
		while held == true
		do
			wait()
			print("holding the key")
		end
		end
end)

UIS.InputEnded:Connect(function(input, GameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.E
	then
		held = false
	end
end)



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