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)
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)
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)
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)
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.
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.
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.