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.

2 Likes

It seems that the issue with your script is that the touched connection is being disconnected immediately after being created, which means that the function ontouched will not be called whenever the hitbox is touched.

Remove the line touchedConn:Disconnect() in ontouched() function. This way, the connection will remain active and ontouched() will be called every time the hitbox is touched.

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)
    if cooldown.Value == false then -- debounce check
        cooldown.Value = true
        hitbox.CanCollide = true
        for i, currentplayer in pairs(game.Players:GetPlayers()) do
            if hitbox.Parent.Name == currentplayer.Name then -- just check the hit player
                local rockcoins = player.leaderstats.rockcoins
                rockcoins.Value = rockcoins.Value + rockamount
                local hum = hitbox.Parent:FindFirstChild("Humanoid")
                local torso = hitbox.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
end

rs.hitboxevent.OnServerEvent:Connect(ontouched)

Nope, i’ve just tried and it still can insta kill if spam click a lot

Did you just remove the touchConn:Disconnect()? Or did you try the whole script that @MrFergxl included?

If you didn’t try the whole script, you should include that if statement in the ontouched() function. You’re not checking if cooldown.Value is true before continuing with the function; if you’re just changing values, it doesn’t matter unless you check for those values.

local function ontouched(--[[Arguments here]])
    if cooldown.Value then return end

    -- Add the rest of the code here.
end

Also, the arguments that you’re sending from the client shouldn’t be sent from the client. Those arguments will affect the gameplay for all the players, so you should find a way to take care of those values from the server – for example, the cooldown can easily be manipulated by an exploiter and the exploiter can make the server think that he/she never has an active cooldown, so he/she can non-stop spam.

Sorry for not responding for soo long, in my time it was 1 am so I had to sleep, i tried the whole script MrFergxl provided.

Sorry for the late response, too – I was also sleeping.

I think that letting us see the LocalScript that’s firing to the server or a portion of it will help.
We should see where the client is calling RemoteEvent:FireServer(), and maybe even where the values that are being passed as arguments through that RemoteEvent:FireServer() are being set.

Edit:
I am believing that your issue is probably that the cooldown BoolValue is sent from the client, or something revolving around that. You shouldn’t receive critical information like that from the client because that information can be easily exploited.

Instead you should get that information from the server, unless the client is supplying a “key” for the server to know what information to access (e.g. if the client tells the server the name of the product wanted to be purchased, then the server uses that “key”, the name of the product, to check if the cost of that product is more money than what the client has or not).

If you accept information from the client that will be used to manipulate the game, make sure it will have no affect until that information has been checked and validated according to the information stored on the server.

dw, i already fixed that, i didnt find the problem but i just fixed it by remaking script
I think you are right, because it fixed when i switched from values to attributes. Thanks!

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