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

How would i make a mapchanger that waits until all the players die before deleting the map?

Asked by 7 years ago

I am not very familiar with getting the names of the players in the server. And in my game i need a map changer that will wait until all the players die, so how would i do this?

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

There are undeniably many solutions to something like this, so I'll just give you some insight on how I'd solve this problem.

Step 1

I'd start by declaring a function that I can store all this code in, which will allow us to make it cleaner, and reusable. We'll call it WaitForPlayerDeaths. As the name implies, our program will have to wait for this function to terminate before executing anything else. So you'll want to use this function sparingly.

Step 2

We want a way to track these player deaths. Some may immediately think of using a loop to constantly check every player's health, but that would be a nasty solution in my opinion. What I'd do instead, is listen for each player's Died event (member of Humanoid).

Progress

local Players = game:GetService("Players")

local function WaitForPlayerDeaths()
    for _, player in next, Players:GetPlayers() do
        --
    end
end

Step 3

Next and last step, we can simply use the Wait method on the Died event signal, which will yield until the event has fired. If we do this for each player, our code will halt until everyone's Died event has fired, meaning we know when everyone has died.

Progress

local Players = game:GetService("Players")

local function WaitForPlayerDeaths()
    for _, player in next, Players:GetPlayers() do
        local character = player.Character -- Get their character
        local human = character and character:WaitForChild("Humanoid") -- Make sure their character exists, and wait for their humanoid
        if human then
            human.Died:Wait() -- Wait for each player to die
        end
    end
end

Awesome, now we can just do whatever we want after knowing all the players have died. You could create a function for this if you want, so you don't have to write two instructions for doing the same thing each time, but that depends on how you're using it and what you want to do.

-- This
WaitForPlayerDeaths()
SomeMap:Destroy()

-- Could turn into this
local function DestroyMapOnPlayerDeaths()
    WaitForPlayerDeaths()
    SomeMap:Destroy()
end

That's all I have planned. Hope I answered your question, if you have any further questions, just let me know.

Ad
Log in to vote
0
Answered by 7 years ago

If you have your pairs loop right, you can use something called "break" and that will break out of the loop!

Example Code:

for i=90,1,-1 do
if script.PlayersLeft.Value == 0 then
break
else
gui.Text = "Insert Text Here"
end

I hoped this helped you and have a good day!!

Answer this question