Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Script to remove health from building not working, no errors, how do I fix???

Asked by 4 years ago

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)

0
Explain your question more please! BashGuy10 384 — 4y
0
A map spawns in workspace. This gets the buildings in the map, checks if a player is touching them, then removes health. There are ~ 200 such buildings. NickIsANuke 217 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 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:

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)
0
Well, in this case, the building would be “hit”, right? NickIsANuke 217 — 4y
0
If so, health would be hit.health NickIsANuke 217 — 4y
0
I actually modified this because in some maps there are multiple models inside of models so instead of using .Parent I used :FindFirstAncestor NickIsANuke 217 — 4y
Ad

Answer this question