local script: Enables a value when the Left arrow key is pressed and Disables the value when the key is released, then makes it so if the value maintains to be true then the remote event keeps repeating and stops when the value becomes false
local uis = game:GetService("UserInputService")
local sm = game.Workspace.valfolder.shootmain --bool val
uis.InputBegan:Connect(function(input)
if input.KeyCode==Enum.KeyCode.Left then
print("left began")
sm.Value = true
end
end)
uis.InputEnded:Connect(function(input)
if input.KeyCode==Enum.KeyCode.Left then
sm.Value = false
print("end left")
end
end)
local status = game.Workspace.valfolder.shootmain
status.Changed:Connect(function()
if status.Value then -- true
repeat print("hi1")
game.ReplicatedStorage.fireshot:FireServer()
wait(0.4)
until not status.Value
else
print("done")
end
end)
server script: Shoots a part from the player's root part and is destroyed when it touches another part. My problem is the remote event can be spammed whenever the key is spammed, how can I add a cooldown?
game.ReplicatedStorage.fireshot.OnServerEvent:Connect(function(player)
local character = player.Character
local shothit = game.ServerStorage:WaitForChild("shothit")
local newshot = shothit:Clone()
local bodyvel = Instance.new("BodyVelocity")
newshot.CFrame = character.HumanoidRootPart.CFrame
bodyvel.Velocity = Vector3.new(0,0,60)
bodyvel.Parent = newshot
newshot.Parent = workspace
local touchconn
touchconn = newshot.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Part") then
print("hit")
if touchconn ~= nil then touchconn:Disconnect() end
wait(1.6)
newshot:Destroy()
end
end)
end)

2 comments
you could make a cooldown table for the server, add a player to it, and then check whether the same player is in the cooldown table or not, and then remove it from the table when the cooldown has passed with the delay function
That sounds like a good idea but tbh I don’t know how to work with tables yet, could you put this into an answer if you feel like it?