My script is supposed to remove the health of a building. It doesn't work. No errors.
Script:
game.Players.PlayerAdded:Connect(function(player) local debris = player:WaitForChild("leaderstats").Debris while true do wait(1) repeat wait (1) until game.Workspace:FindFirstChild("Map") local ClonedMap = game.Workspace:WaitForChild("Map") wait(2) for i,v in pairs(ClonedMap.Buildings:GetChildren()) do for a,k in pairs(v:GetChildren()) do if k:IsA("Part") or k:IsA("BasePart") then k.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then v:FindFirstChild("Health").Value = v:FindFirstChild("Health").Value - player:FindFirstChild("Morph").Damage.Value if v:FindFirstChild("Health").Value == 0 then v:Destroy() print("Building destroyed.") end player.leaderstats.Debris.Value = player.leaderstats.Debris.Value + 10 end end) end end end end end)
This is a common mistake that I see in many people's scripts and that is trying to connect events in loops for you the while true do loop doesn't serve a purpose in your script. I also notice that your touch events are based on the building's parts which should be detecting a if a player's baseparts are touching a building parts. The main thing that should be done is separating theses events and functions and not combining them. I can simplify the script to be this:
game.Players.PlayerAdded:Connect(function(player) local debris = player:WaitForChild("leaderstats").Debris player.CharacterAdded:Connect(function(character) for __, Part in pairs(character:GetChildren()) do if Part:IsA("BasePart") then Part.Touched:Connect(function(hit) if hit.Parent.Parent.Name == "Map" then hit:FindFirstChild("Health").Value = hit:FindFirstChild("Health").Value - player:FindFirstChild("Morph").Damage.Value if hit:FindFirstChild("Health").Value == 0 then hit:Destroy() print("Building destroyed.") end player.leaderstats.Debris.Value = player.leaderstats.Debris.Value + 10 end end) end end end) end)
If the morph is in the character then try this:
game.Players.PlayerAdded:Connect(function(player) --[[local LS = Instance.new("Folder") LS.Parent = player LS.Name = "leaderstats" local DB = Instance.new("IntValue") DB.Parent = LS DB.Name = "Debris"]] local debris = player:WaitForChild("leaderstats").Debris player.CharacterAdded:Connect(function(character) for __, Part in pairs(character:GetChildren()) do if Part:IsA("BasePart") then Part.Touched:Connect(function(hit) if hit.Parent.Parent.Name == "Map" then hit.Parent.Health.Value = hit.Parent:FindFirstChild("Health").Value - character:FindFirstChild("Morph").Damage.Value if hit.Parent:FindFirstChild("Health").Value == 0 then hit.Parent:Destroy() print("Building destroyed.") end player.leaderstats.Debris.Value = player.leaderstats.Debris.Value + 10 end end) end end end) end)