Title. In the videos below, the paintball gun has a cooldown of .5 seconds before it can be used again, while the superball’s cooldown lasts for 2 seconds.
In this video where I’m just testing out the tools in Studio, everything works absolutely fine…
…But in this video where I’m playtesting on a server in Studio, the cooldowns are obviously not applying correctly. Stranger still is that when I go into an actual online server, the tools only activate once and the cooldown never expires.
I’ve disabled every other script in the game and the problem persists, so I’m confident that it’s the scripts in the tools that’s causing the issue. In the second video, the superball’s cooldown seems to last around 1.5 seconds while the paintball gun can fire again instantly, so I think the bug has to do with the two values affecting each other somehow.
Here’s the relevant code. Both of these snippets are identical in the scripts of both tools; the MaxCooldown value is a fixed number while Cooldown is based on tick(). The cooldown is visualized through the tool icons in the custom backpack GUI.
Client
local tool = script.Parent
local events = tool:WaitForChild("Events")
local click_event = events:WaitForChild("Click")
tool.Activated:Connect(function()
if tick() - tool:GetAttribute("Cooldown") >= tool:GetAttribute("MaxCooldown") then
tool:SetAttribute("Cooldown", math.huge) --prevents event from firing again too early
click_event:FireServer(mouse.Hit.Position)
end
end)
Server
local tool = script.Parent
local events = tool:WaitForChild("Events")
local click_event = events:WaitForChild("Click")
click_event.OnServerEvent:Connect(function(plr, mouse_pos) --main function
tool:SetAttribute("Cooldown", tick())
end)