You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to make cooldown so it doesnt damage or knockback too fast
What is the issue? Include screenshots / videos if possible!
It simply doesnt work, I can spam click to insta kill the character
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried checking if im using right instances or making the cooldown set to true only in touched event, but nothing changed
Here’s the script
local rs = game:GetService("ReplicatedStorage")
local rockamount = 1
local function takeDamage(hum, torso, player, damage, power)
hum:TakeDamage(damage)
local char = player.Character or player.CharacterAdded:Wait()
local facepart = char:FindFirstChild("FacePart")
local knockback = Instance.new("BodyVelocity")
knockback.Velocity = facepart.CFrame.LookVector * power
knockback.P = 5000
knockback.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
knockback.Parent = torso
task.wait(0.1)
knockback:Destroy()
end
local function ontouched(player, power, damage, speed, hitbox, cooldown)
cooldown.Value = true
hitbox.CanCollide = true
local touchedConn = hitbox.Touched:Connect(function(part)
for i, currentplayer in pairs(game.Players:GetPlayers()) do
if part.Parent.Name == currentplayer.Name and cooldown.Value == false then
cooldown.Value = true
local rockcoins = player.leaderstats.rockcoins
rockcoins.Value = rockcoins.Value + rockamount
local hum = part.Parent:FindFirstChild("Humanoid")
local torso = part.Parent:FindFirstChild("Torso")
if hum and torso and hum.Health > 0 then
takeDamage(hum, torso, player, damage, power)
end
end
end
task.wait(speed)
hitbox.CanCollide = false
cooldown.Value = false
end)
touchedConn:Disconnect()
end
rs.hitboxevent.OnServerEvent:Connect(ontouched)
You can use :Once() instead of connect if you only want it to happen once! Also, I notice you aren’t using i, anywhere, so you can replace it with _ to indicate it isn’t used. (for _, currentplayer …)
I also don’t think the client should tell the server what power, damage, speed, hitbox or cooldown it is. Exploiters may for example send a remote event to the server with the following:
Protecting from exploiters isn’t always super necessary, but it usually leads to better code as well. It makes things easier to implement imo.
On the client you just do hitboxevent:Fire() and you are done. On the server you can then have a clear and easy way to determine the rest of the values.
-- Pseudo code
OnServerEvent:Connect(function(player)
-- Get their cooldown, ex:
local cooldown = player.Cooldown
if cooldown.Value then return end -- Player cannot use this yet, still on cooldown
-- Get their speed from wherever that is stored, ex:
local speed = player.Speed.Value
cooldown.Value = true
task.delay(speed, function() -- Clear cooldown after ``speed`` seconds.
cooldown.Value = false
end)
... -- Whatever else is done
end)
Sidenote: I recommend using attributes instead of value objects, they are easy to use too.
-- Value object:
local storedValue = object.Value
object.Value = newValue
-- Attributes:
local storedValue = object:GetAttribute("AttributeName")
object:SetAttribute("AttributeName", newValue)
You don’t have to, but it can be easier that way! If their stats are tied to the player, set the stats on the player. If they are tied to a tool, set the stats on the tool.
I don’t see anything immediately that is wrong with your current script, but if you change to a better structure you might solve it simultaneously.