Stop tool from activating when player switches item

tool = script.Parent
players = tool.Parent
eated = false
canvalue = 10

playerSanity = game:GetService("ReplicatedStorage").Sanity

game.Players.PlayerAdded:Connect(function(player)
	players = player
end)

tool.Activated:Connect(function()
	wait(3)
	eated = true
	if eated == true then
		playerSanity:Fire(players,canvalue)
		script.Parent:Destroy()
	end
end)

FYI : THIS IS A SERVER SCRIPT

I want the player to need to wait for 3 seconds after tool is activated or else the tool will not be activated. Please help me!

Putting a wait in the middle of a function will not act as a cooldown, since the activating of the function in your code only checks if something is .Activated, and not if there is a cooldown.

To fix this, make a local variable debounce = false at the top of the script, this will be what determines if the cooldown is active or not.

After that you want to activate a delay after it has been activated, so after the function, you activate the cooldown by adding debounce = true.

Since you don’t want a delay in your code, you don’t set a task.wait() until the last line in the function, setting it to the amount of seconds you want the delay to take, in your case being 3.

Then revert the cooldown to false with debounce = false.

Last thing to do is make the function check for a cooldown, which you can add in before the cooldown = true with if cooldown == true then return print("Cooldown.") end

I also removed your eated variable, since I am assuming it was your placeholder cooldown.


End result:

tool = script.Parent
players = tool.Parent
canvalue = 10
debounce = false

playerSanity = game:GetService("ReplicatedStorage").Sanity

game.Players.PlayerAdded:Connect(function(player)
	players = player
end)

tool.Activated:Connect(function()
	if debounce == true then return print("Cooldown") end
	debounce = true
	playerSanity:Fire(players,canvalue)
	script.Parent:Destroy()
	task.wait(3)
	debounce = false
end)