Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Would I Destroy A Part When Everyone Dies?

I Cannot Think On How To Destroy A Part When Someone Dies, Plz Give Me Tips On How To Do IT i tried to script it but im only a beginning so i have no idea what im doing xD Plz HELP!

local Player = game.Players:GetPlayers()
local Humanoid = Player:WaitForChild("Humanoid")
if Humanoid.Health == 0 then
	script.Parent:Destroy()
end

this is the error attempt to call method 'WaitForChild' (a nil value) i have no idea what to use instead?

2 comments

0

You have multiple problems with your code; 1. You are attempting to call 'WaitForChild' on a table. 2. 'GetPlayers' creates a table of the current Player's in the 'Players' Service, thus your problem. 3. The 'if' statement will only work once in the code, consider using the '.Died' event upon a Player's death?

0

so how to fix it?

1 answer

✓ Accepted answer

Sorry I couldn't explain this to you earlier, I was short on time.

local Part = workspace.Part--assign "Part" the value of a part under the name "Part" in workspace

local function armageddon() --A function that returns true when all the players have died after a certain point
local alive = game.Players:GetPlayers()--create a list of all the players named "alive"
for i,player in pairs(alive) do --a generic for loop that iterates through values of the table "alive"
coroutine.resume(coroutine.create(function(num,player)--create a new thread with "num" and "player" as arguments
if not player.Character then--if the player is already dead or doesn't have a character,run the following code
table.remove(alive,num)--removes the player from the table
return--stopes the coroutine from running
end--end the if statement
local died --define "died" locally
died = player.Character:FindFirstChild("Humanoid").Died:connect(function() --assign "died" an event that runs when the player dies
died:disconnect()--disconnects the "died" event, so it only checks if the player died once
table.remove(alive,num)--removes the player from the table
end)--ends the event
end),i,player)--ends the coroutine and assigns it the values "i" and "player", being the iteration and the value.
end--ends the for loop
while #alive > 0 do--run the loop to block the rest of the code from running while at least one player is alive
wait()--waits a small amount of time to prevent the game from crashing
end--ends the while loop
return true;--function returns true
end--ends the function

if armageddon() then --call the function when you want it to run
Part:Destroy() --destroys the part
end--ends the if statement

2 comments

0

thanks that helped

0

I edited my answer with an explanation. Sorry I couldn't provide on earlier; I was low on time. I also made the code more efficient and tested it. If you want some URLs or something, just comment. Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.