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 5 years ago

My script is supposed to remove the health of a building. It doesn't work. No errors.

Script:

01game.Players.PlayerAdded:Connect(function(player)
02    local debris = player:WaitForChild("leaderstats").Debris
03    while true do
04        wait(1)
05 
06        repeat wait (1) until game.Workspace:FindFirstChild("Map")
07 
08        local ClonedMap = game.Workspace:WaitForChild("Map")
09 
10        wait(2)
11 
12        for i,v in pairs(ClonedMap.Buildings:GetChildren()) do
13            for a,k in pairs(v:GetChildren()) do
14                if k:IsA("Part") or k:IsA("BasePart") then
15                    k.Touched:Connect(function(hit)
View all 30 lines...
0
Explain your question more please! BashGuy10 384 — 5y
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 — 5y

1 answer

Log in to vote
1
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:

01game.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
10                            hit:Destroy()
11                            print("Building destroyed.")
12                        end
13                        player.leaderstats.Debris.Value = player.leaderstats.Debris.Value + 10
14                    end
15                end)
16            end
17        end
18    end)
19end)

If the morph is in the character then try this:

01game.Players.PlayerAdded:Connect(function(player)
02    --[[local LS = Instance.new("Folder")
03    LS.Parent = player
04    LS.Name = "leaderstats"
05    local DB = Instance.new("IntValue")
06    DB.Parent = LS
07    DB.Name = "Debris"]]
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
View all 25 lines...
0
Well, in this case, the building would be “hit”, right? NickIsANuke 217 — 5y
0
If so, health would be hit.health NickIsANuke 217 — 5y
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 — 5y
Ad

Answer this question