Remote Event Firing multiple times

I’m making a tank that moves based on W and S but my remote events are firing more than once when I click once which causes it to accelerate at huge speeds in one click. How would I make the remote event to fire only once per click?

Local Script

		UIS.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.W then
				game.ReplicatedStorage.ReversedF:FireServer()
				print('Fired')
			end
			if input.KeyCode == Enum.KeyCode.S then
				game.ReplicatedStorage.ReversedB:FireServer()
				print('Fired!')
			end
		end)

Server

game.ReplicatedStorage.ReversedF.OnServerEvent:Connect(function(player)
	_G.Factor += 10
end)

game.ReplicatedStorage.ReversedB.OnServerEvent:Connect(function(player)
	_G.Factor -= 10
end)

Thanks

2 Likes

try to add a debounce like this:

local script:

UIS.InputBegan:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.W then
		if db == false then
			db = true
			game.ReplicatedStorage.ReversedF:FireServer()
			print('Fired')
		end
	end
end)
game.ReplicatedStorage.ReversedF.OnClientEvent:Connect(function()
	wait(cooldown) --change cooldown to whatever seconds u want
	db = false
	print('cooldown finished, can input w again')
end)

local db2 = false
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.S then
		if db2 == false then
			db2 = true
			game.ReplicatedStorage.ReversedB:FireServer()
			print('Fired!')
		end
	end
end)
game.ReplicatedStorage.ReversedB.OnClientEvent:Connect(function()
	wait(cooldown) --change cooldown to whatever seconds u want
	db2 = false
	print('cooldown finished, can input s again')
end)

script:

game.ReplicatedStorage.ReversedF.OnServerEvent:Connect(function(player)
	_G.Factor += 10
	game.ReplicatedStorage.ReversedF:FireClient(player)
	print("fired client")
end)

game.ReplicatedStorage.ReversedB.OnServerEvent:Connect(function(player)
	_G.Factor -= 10
	game.ReplicatedStorage.ReversedB:FireClient(player)
	print("fired client")
end)
3 Likes

I think your _G.Factor is too high. If you press “W” once, the remote event is only firing once.

1 Like

You can use a remote function, so you don’t have to fire the remote event from server to client.

UIS.InputBegan:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.W then
		if db == false then
			db = true
			game.ReplicatedStorage.ReversedF:InvokeServer()
            wait(cooldown)
	        db = false
	        print('cooldown finished')
		end
	end
end)
2 Likes

That made the program print “Fired” and “Fired!” only once when clicked, didn’t work after that even with a 1 second cooldown.

1 Like

My Factor is actually 0. It adds 10 or subtracts 10 based on the input.

2 Likes

I think checking if the user input state is equal to begin (a key was only pressed down) could fix this issue.

UIS.InputBegan:Connect(function(input)
	local pressedDown = input.UserInputState == Enum.UserInputState.Begin
	if input.KeyCode == Enum.KeyCode.W and pressedDown then
		game.ReplicatedStorage.ReversedF:FireServer()
		print('Fired')
	end
	if input.KeyCode == Enum.KeyCode.S and pressedDown then
		game.ReplicatedStorage.ReversedB:FireServer()
		print('Fired!')
	end
end)
2 Likes

After testing your scripts in Studio, I found no problems whatsoever. The issue is most likely somewhere else. If you can, please post more lines from the local script.

1 Like

Thank you for your help but I figured out the solution. I wanted to make when the key was held and not fire multiple times so I just added a boolen parameter to the remote event and a server script that works accordingly.

1 Like
local userInput = game:GetService("UserInputService")

userInput.InputBegan:Connect(function(startKey, processed)
	if processed then return end
	local startTime = tick()

	local connection do
		connection = userInput.InputEnded:Connect(function(endKey, processed)
			if processed then return end

			if startKey == endKey then
				connection:Disconnect()
				local endTime = tick()
				local diffTime = math.round((endTime - startTime) * 100) / 100
				print("Key: "..startKey.KeyCode.Name.." was held for "..diffTime.." seconds!")
			end
		end)
	end
end)

This is how you could go about achieving key held/key released behavior.

2 Likes