I can't make it so that touched event wont work after 3 seconds

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 can safely yield inside the OnServerEvent function for 3 seconds and disconnect the touched connection.

hitboxconnect = rs.hitboxevent.OnServerEvent:Connect(function(player, tool)
	local 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)
    task.wait(3)
    touched:Disconnect()
end)

Thank you! I thought that stuff after events not gonna be runned until the event is activated!

1 Like

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