Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I want to get a cool down on my weapons. But how?

This is the script: local tool = script.Parent local canDamage = false

local function onTouch(otherPart)

local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

if not humanoid then 
	return 
end

if humanoid.Parent ~= tool.Parent and canDamage then 
	humanoid:TakeDamage(25)
else
	return
end

canDamage = false

end

local function slash() local str = Instance.new("StringValue") str.Name = "toolanim"--tool animation str.Value = "Slash" str.Parent = tool canDamage = true end

tool.Activated:Connect(slash) tool.Handle.Touched:Connect(onTouch)

it has a local script too but that's for the animation. I want it to have the cooldown because if it doesn't people will be spam clicking and instakilling other players.

1 answer

You can make a variable on the top and it will refer to a number. Then, add wait(thevariable) before canDamage = false. If you are confused, this is what I mean.

cooldown = 3   --You make this at the top so you can adjust it more convenient

local tool = script.Parent 
local canDamage = false
local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

if not humanoid then 
	return 
end

local function onTouch(otherPart)
	if humanoid.Parent ~= tool.Parent and canDamage then   --You check if the player can attack here
		canDamage = false   --We make the player cannot attack once they activate the tool.
		humanoid:TakeDamage(25)
		wait(cooldown)   --Then, wait the 3 seconds then we make the player can attack again.
		canDamage = true   --After that, we make the player can attack again.
	else
		return
	end
end

local function slash() 
	local str = Instance.new("StringValue") 
	str.Name = "toolanim"
	str.Value = "Slash" 
	str.Parent = tool 
	canDamage = true
end

tool.Activated:Connect(slash) 
tool.Handle.Touched:Connect(onTouch)

13 comments

0

thanks

0

i just didnt want people to spam click

0

Uh, that's how you can solve it.

0

You want explanation?

0

sure and in studio it isnt working

0

wait maybe i could let you into the game in studio and you do the code for the cooldown?

0

Sure, I guess..

0

Wait, try the script again. I think it should work now.

0

i enabled collaborative editing but when i go to the permissions page it says i need to turn it on...

0

I edited my answer, I think the script should be able to work now.

0

ok i have to go for a little bit

0

its not damaging

0

it didnt work btw

This discussion is preserved as it was. New answers, comments, and votes are closed.