Code that I can't fix

Basically I want this code to work for next 3 seconds (like if OnServerEvent then there gonna be 3 seconds for touch event to fire, or else you will need to OnServerEvent again)
Here is the code

local rs = game:GetService("ReplicatedStorage")
local rockamount = 1

local function takeDamage(player, tool, hum, torso, char, anotherchar)
	hum:TakeDamage(tool:GetAttribute("damage"))
	local facepart = anotherchar:FindFirstChild("FacePart")
	local knockback = Instance.new("BodyVelocity")
	knockback.Velocity = facepart.CFrame.LookVector * tool:GetAttribute("power")
	knockback.P = 5000
	knockback.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	knockback.Parent = torso
	game:GetService("Debris"):AddItem(knockback, .1)
end

local touched
local hitboxconnect

hitboxconnect = rs.hitboxevent.OnServerEvent:Connect(function(player, tool)
	touched = tool.Hitbox.Touched:Connect(function(hit)
		if tool:GetAttribute("cooldown") == false and hit.Parent.Humanoid and hit.Parent.Torso and hit.Parent.Humanoid.Health > 0 and hit.Parent.Name ~= player.Name and CanHit  then
			touched:Disconnect()
			local char = hit:FindFirstAncestorWhichIsA("Model")
			local anotherchar = player.Character or player.CharacterAdded:Wait()
			local rockcoins = player.leaderstats.rockcoins
			local hum = char.Humanoid
			local torso = char.Torso

			tool.Hitbox.CanCollide = true
			tool:SetAttribute("cooldown", true)
			rockcoins.Value += rockamount


			takeDamage(player, tool, hum, torso, char, anotherchar)
			task.wait(tool:GetAttribute("speed"))


			tool.Hitbox.CanCollide = false
			tool:SetAttribute("cooldown", false)
			
		else
			touched:Disconnect()
		end
	end)
end)

Thanks.

1 Like

You have a line in your code with a cooldown:

tool:SetAttribute("cooldown", true)

What is the cooldown? It should be a number that maybe you can change to 3.

Otherwise, use wait(3) at the location in the script where you want it to wait.

NOTE: task,wait() is the new way but I have found sometimes it completely stops my scripts so I frequently keep using wait() instead.

Okay i will try that, sorry for telling only in 2 hours because i was playing

I tried to remake script listening to you but it completely broke, why?

local rs = game:GetService("ReplicatedStorage")
local rockamount = 1

local function takeDamage(player, tool, hum, torso, char, anotherchar)
	hum:TakeDamage(tool:GetAttribute("damage"))
	local facepart = anotherchar:FindFirstChild("FacePart")
	local knockback = Instance.new("BodyVelocity")
	knockback.Velocity = facepart.CFrame.LookVector * tool:GetAttribute("power")
	knockback.P = 5000
	knockback.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	knockback.Parent = torso
	game:GetService("Debris"):AddItem(knockback, .1)
end

local function cooldownFunction(tool)
	for i = 0,3 do
		task.wait(1)
		tool:SetAttribute("cooldown", 3)
	end
end

local touched
local hitboxconnect

hitboxconnect = rs.hitboxevent.OnServerEvent:Connect(function(player, tool)
	cooldownFunction(tool)
	touched = tool.Hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("Torso") and hit.Parent:FindFirstChild("Humanoid").Health > 0 and hit.Parent.Name ~= player.Name then
			if tool:GetAttribute("cooldown") ~= 3 then
				touched:Disconnect()
				local char = hit:FindFirstAncestorWhichIsA("Model")
				local anotherchar = player.Character or player.CharacterAdded:Wait()
				local rockcoins = player.leaderstats.rockcoins
				local hum = char.Humanoid
				local torso = char.Torso

				tool.Hitbox.CanCollide = true
				tool:SetAttribute("cooldown", true)
				rockcoins.Value += rockamount


				takeDamage(player, tool, hum, torso, char, anotherchar)
				task.wait(tool:GetAttribute("speed"))


				tool.Hitbox.CanCollide = false
				tool:SetAttribute("cooldown", false)
			end

		else
			touched:Disconnect()
		end
	end)
end)

Also again sorry for sending in couple of hours, I played again

I found the solution by creating new post, but thanks for trying to help! :smiley:

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