Not doing damage after respawn?

I’ve run into this problem and I don’t know how to fix it. It’s like, you destroy the tree, and then the tree respawns, but, then, you can’t seem to do any damage to it anymore, anyone that can help me fix this problem? (the duplicated tree is identical to the one destroyed)
Video:


And here’s the damage script:

game.Players.PlayerAdded:Connect(function(plr)
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local Dmg = ReplicatedStorage:WaitForChild("Damage")
	local Child = script.Parent.Parent:GetChildren()
	local hitpoint = false
	local db = false
	
	script.Parent.Touched:Connect(function(hit)
		if hit.Parent.Name == "Tool" then
			local Stats = plr:WaitForChild("Stats")
			if Stats ~= nil then
				if script.Parent.Health.Value > 0 then
					local Damage = Stats:WaitForChild("Damage")
					local Tool = Stats:WaitForChild("Tool")
					if Damage and Tool ~= nil then
						Dmg.OnServerEvent:Connect(function()
							hitpoint = true
						end)
						if Tool.Value == "Axe" then
							local Dam = ReplicatedStorage:WaitForChild("Damage")
							if hitpoint == true then
								script.Parent.Health.Value = script.Parent.Health.Value - Damage.Value
								hitpoint = false
							end
							if script.Parent.Health.Value <= 0 then
								for i, v in pairs(Child) do
									if v.IsAPart then
										v:Destroy()
									end
								end
							end
						else
							if hitpoint == true then
								script.Parent.Health.Value = script.Parent.Health.Value - 1
								hitpoint = false
							end
							if script.Parent.Health.Value <= 0 then
								for i, v in pairs(Child) do
									if v.IsAPart then
										v:Destroy()
									end
								end
							end
						end
					end
				end
			end
		end
	end)
end)

Here’s the regen script:

script.Parent.ChildRemoved:Connect(function()
	wait(1)
	local Cloned = game.ServerStorage.Tree:GetChildren()
	for i, v in pairs(Cloned) do
		v:Clone()
		v.Parent = script.Parent
	end
end)

Here’s where the scripts are(the “tree” models parent is the workspace)
Capture

The issue here is that you are linking the function to game.Players.PlayerAdded

You are only able to damage the tree if you join the server while this script is listening for you to join. When you use the regen script, the damage script is reloaded and everyone currently in the server is ignored.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Dmg = ReplicatedStorage:WaitForChild("Damage")
local Child = script.Parent.Parent:GetChildren()
local hitpoint = false
local db = false
	
script.Parent.Touched:Connect(function(hit)
-- get player from part, instead of playeradded
  local plr = game.Players:GetPlayerFromCharacter(hit.Parent) 
  if plr == nil then
    return
  end
		
-- execute rest of code with new plr variable
  if hit.Parent.Name == "Tool" then
			local Stats = plr:WaitForChild("Stats")
			if Stats ~= nil then
				if script.Parent.Health.Value > 0 then
					local Damage = Stats:WaitForChild("Damage")
					local Tool = Stats:WaitForChild("Tool")
					if Damage and Tool ~= nil then
						Dmg.OnServerEvent:Connect(function()
							hitpoint = true
						end)
						if Tool.Value == "Axe" then
							local Dam = ReplicatedStorage:WaitForChild("Damage")
							if hitpoint == true then
								script.Parent.Health.Value = script.Parent.Health.Value - Damage.Value
								hitpoint = false
							end
							if script.Parent.Health.Value <= 0 then
								for i, v in pairs(Child) do
									if v.IsAPart then
										v:Destroy()
									end
								end
							end
						else
							if hitpoint == true then
								script.Parent.Health.Value = script.Parent.Health.Value - 1
								hitpoint = false
							end
							if script.Parent.Health.Value <= 0 then
								for i, v in pairs(Child) do
									if v.IsAPart then
										v:Destroy()
									end
								end
							end
						end
					end
				end
			end
		end
	end)
end)
2 Likes

Oh, thank you! I thought that the script will fire when it goes in the workspace, because it detects new players(It was in ServerStorage). But, then how do you reference the player without having to have a nil value error? The hit is referencing the rock that hit it. Not the player.