Hitbox script doesnt really 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 make the hitbox damage and knockback the player when i activate the tool

  1. What is the issue? Include screenshots / videos if possible!

It almost works as needed, but when I activate it at least once, it starts to hit the player even when not activated if touched

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried removing or adding some pieces of code and nothing changed

Here’s the local script

local tool = script.Parent.Parent
local rs = game:GetService("ReplicatedStorage")

tool.Activated:Connect(function()
	rs.hitboxevent:FireServer(tool)
end)

And server script

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 = nil
local hitboxconnect = nil

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 then
			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)
			
			touched:Disconnect()
			hitboxconnect:Disconnect()
		else
			touched:Disconnect()
			hitboxconnect:Disconnect()
		end
	end)
end)

I also have another problem, with rock setting system. I will just provide script and tell you what’s wrong, you can to not help with that if not want

Here

local rs = game:GetService("ReplicatedStorage")


game.Players.PlayerAdded:Connect(function(plr)
	local rock = Instance.new("StringValue")
	rock.Name = "rock"
	rock.Value = "default"
	rock.Parent = plr:WaitForChild("leaderstats")
end)


local changeconn = nil


task.spawn(function()
	while task.wait() do
		for _, player in pairs(game.Players:GetPlayers()) do
			local rock = player.leaderstats:WaitForChild("rock")
			rock.Changed:Connect(function()
				local therock = rock.Name.." rock"
				for _, currentrock in pairs(game.ServerStorage.rocks:GetChildren()) do
					if currentrock.Name == therock then
						local char = player.Character or player.CharacterAdded:Wait()
						for _, delete in pairs(char:GetChildren()) do
							if delete.Name ~= therock and delete:IsA("Tool") then
								delete:Destroy()
							end
						end
						
						for _, delete2 in pairs(player.Backpack:GetChildren()) do
							if delete2.Name ~= therock and delete2:IsA("Tool") then
								delete2:Destroy()
							end
						end
						
						for _, delete3 in pairs(player.StarterGear:GetChildren()) do
							if delete3.Name ~= therock and delete3:IsA("Tool") then
								delete3:Destroy()
							end
						end
						
						local clonedtool = currentrock:Clone()
						clonedtool.Parent = player.Backpack
						local clonedtool2 = currentrock:Clone()
						clonedtool2.Parent = player.StarterGear
					end
				end
			end)
		end
	end
end)


for i, v in pairs(workspace.spawn.stands:GetChildren()) do
	-- spawns
	v:FindFirstChild("ClickDetector").MouseClick:Connect(function(plr)
		-- clickdetector event
		if v:GetAttribute("price") <= plr:FindFirstChild("leaderstats").rockcoins.Value then
			-- if price smaller than rockcoins amount
			plr:FindFirstChild("leaderstats").rock.Value = v.Name
			for _, tool in pairs(game.ServerStorage.rocks:GetChildren()) do
				--all items in game
				if v:GetAttribute("rock") == tool.Name then
					-- if item name is the same
					for _, starterpack in pairs(game.StarterPack:GetChildren()) do
						--starterpack item
						starterpack:Destroy()
					end
					
					for _, deletetool in pairs(plr.Backpack:GetChildren()) do
						for _, startergear in pairs(plr.StarterGear:GetChildren()) do
							startergear:Destroy()
						end
						-- tool that needs to be deleted
						if deletetool:IsA("Tool") then
							--if tool is in backpack
							deletetool:Destroy()
						else
							for _, thedeletetool in pairs(plr.Character:GetChildren()) do
								--if tool is in character
								if deletetool:IsA("Tool") then
									deletetool:Destroy()
								end
							end
						end
					end
					local backpack = plr.Character or plr.Backpack
					local backpackprob = plr.Backpack
					local clonedtool = tool:Clone()
					local anotherclonedtool = tool:Clone()
					clonedtool.Parent = plr.Backpack
					anotherclonedtool.Parent = plr.StarterGear
				end
			end
		end
	end)
end

The problem with 2nd script is in task.spawn function, it should change the rock to the rock I need when value of rock changed, but it doesnt work, but other’s script do work, dont mind them

Thanks.