Cooldown doesnt work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to make cooldown so it doesnt damage or knockback too fast

  1. What is the issue? Include screenshots / videos if possible!

It simply doesnt work, I can spam click to insta kill the character

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

Thanks.

you must create a debounce value

What you mean by thedebounce?

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:

rs.hitboxevent:Fire(math.huge, math.huge, math.huge, workspace.BasePlate, <another players cooldown>)

Instead, the client should send an event to the server with the bare minimum information. The rest should be done on the server.

Nah its ok, this project is biggest for me but not so important to protect it from exploiters, also i will try :Once(), thanks

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)

Oh, i see, so i should store the values inside the player for easier scripting? I didnt even think about that, thanks.

but for some reason the script doesnt work still, like it doesnt damage or knockback

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.

Best of luck!

1 Like

thanks, i will try to rescript it then i guess. :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.