Answered by
5 years ago Edited 5 years ago
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:
01 | game.Players.PlayerAdded:Connect( function (player) |
02 | local debris = player:WaitForChild( "leaderstats" ).Debris |
03 | player.CharacterAdded:Connect( function (character) |
04 | for __, Part in pairs (character:GetChildren()) do |
05 | if Part:IsA( "BasePart" ) then |
06 | Part.Touched:Connect( function (hit) |
07 | if hit.Parent.Parent.Name = = "Map" then |
08 | hit:FindFirstChild( "Health" ).Value = hit:FindFirstChild( "Health" ).Value - player:FindFirstChild( "Morph" ).Damage.Value |
09 | if hit:FindFirstChild( "Health" ).Value = = 0 then |
11 | print ( "Building destroyed." ) |
13 | player.leaderstats.Debris.Value = player.leaderstats.Debris.Value + 10 |
If the morph is in the character then try this:
01 | game.Players.PlayerAdded:Connect( function (player) |
08 | local debris = player:WaitForChild( "leaderstats" ).Debris |
09 | player.CharacterAdded:Connect( function (character) |
10 | for __, Part in pairs (character:GetChildren()) do |
11 | if Part:IsA( "BasePart" ) then |
12 | Part.Touched:Connect( function (hit) |
13 | if hit.Parent.Parent.Name = = "Map" then |
14 | hit.Parent.Health.Value = hit.Parent:FindFirstChild( "Health" ).Value - character:FindFirstChild( "Morph" ).Damage.Value |
15 | if hit.Parent:FindFirstChild( "Health" ).Value = = 0 then |
17 | print ( "Building destroyed." ) |
19 | player.leaderstats.Debris.Value = player.leaderstats.Debris.Value + 10 |