My UIS event fires twice

So what I am making is a hold button event, as I was trying to make a system where if you hold down a key, while holding it down some events happen.

Now the issue is is that for somereason what I believe to be the error is that the InputBegan fucntion fires twice and I have no idea why.

Script
local UIS = game:GetService("UserInputService")
local Hector3 = {0, 0, 0} 
local Ector3 = workspace.Rocket.VectorForce

local RS = game:GetService("ReplicatedStorage")

local KeyDown = false
local function onInputBegan(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.A then
			KeyDown = true
			repeat wait()
				RS.Increase:FireServer()
				print("Starting")
			until KeyDown == false
		end
	end
end

local function onInputEnded(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.A and gameProcessed == false then		
			KeyDown = false
			print("Ended")
			RS.Decrease:FireServer()
			
		end
	end
end

UIS.InputBegan:Connect(onInputBegan)
UIS.InputEnded:Connect(onInputEnded)



https://gyazo.com/97a7c03188470233049e963dfd2e6c8e

As you can see in the gif, when it prints many times, it means that the button is being held, when you let go, it should just print “Ended”, however it also prints “Starting”.

Any help would be appreciated thanks

The issue is most likely the fact you are using a repeat. Pretty sure repeats go through the content of the repeat and only check the condition AFTER the repeat is finished. Try using a while loop instead.

local KeyDown = false
local function onInputBegan(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.A then
			KeyDown = true
			while KeyDown do
				RS.Increase:FireServer()
				print("Starting")
				wait()
			end
		end
	end
end

Actually, coming back to this topic, it could just be because the wait is at the beginning of the repeat. meaning that the condition was valid just a few milliseconds beforehand.

You don’t need KeyDown as UserInputService has the IsKeyDown method.

Also, fixing up your script, you can try this:

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Hector3 = {0, 0, 0} 
local Ector3 = workspace.Rocket.VectorForce

function InputBegan(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.keyCode == Enum.KeyCode.A and not gameProcessed then
        print("Starting")
        while UserInputService:IsKeyDown(Enum.KeyCode.A) do
            ReplicatedStorage.Increase:FireServer()
            task.wait()
        end

        if UserInputService:IsKeyDown(Enum.KeyCode.A) == false and not gameProcessed then
            print("Ended")
            ReplicatedStorage.Decrease:FireServer()
        end
	end
end

UserInputService.InputBegan:Connect(InputBegan)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.