I wanted to add a cooldown to the firing of the gun I was making, but the cooldown doesn’t work…
I’ve looked at some possible solutions, but none work. It might have something to do with the remote event or it being a while true loop, but I can’t think of anything that could fix it.
(localscript in gun)
local tool = script.Parent.Parent
local shots = 0
local mouse = game.Players.LocalPlayer:GetMouse()
local firing = false
local replicatedstorage = game:GetService("ReplicatedStorage")
local remoteevent = replicatedstorage:WaitForChild("BallistaFire")
local spin = 0
local humanoider = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
tool.Equipped:Connect(function(mouse)
local player = game.Players.LocalPlayer
mouse.Button1Down:Connect(function()
firing = true
while firing do
firing = false
script.Parent.Shoot_Sound:Play()
remoteevent:FireServer(tool.Handle.Position , mouse.Hit.p)
wait(5)
firing = true
end
end)
mouse.Button1Up:Connect(function()
end)
end)
(serverscript in serverscriptservice)
local replicatedstorage = game:GetService("ReplicatedStorage")
local remoteevent = replicatedstorage:WaitForChild("BallistaFire")
remoteevent.OnServerEvent:Connect(function(player, gunPos, mousePos)
local bullet = replicatedstorage:FindFirstChild("Bullet")
local bulletclone = bullet:Clone()
bulletclone.Parent = game.Workspace
local speed = 500
bulletclone.CFrame = CFrame.new(mousePos)
bulletclone.Touched:Connect(function(hit)
local humanoidhit = hit.Parent:FindFirstChild("Humanoid")
if humanoidhit ~= nil then
if hit.Parent.Name ~= player.Name then
humanoidhit:TakeDamage(100)
bulletclone:Destroy()
end
end
end)
end)