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

How can I make it so when a there is only one player alive the loop breaks?

Asked by 4 years ago
Edited 4 years ago
while true do


game.Workspace.BackGroundMusic:Play()
local text = game.ReplicatedStorage.TextValue
local plrsleft = #game.Players:GetChildren()
local pL = game.Players

        while true do
        local plrsleft = #game.Players:GetChildren()
        print("Not enough players...")
        text.Value = "Not enough players..."
        wait(1)
        if plrsleft >= 1 then 
        wait(1)
        break
    end
end

while true do
text.Value = "Intermission"
print("Intermission")
wait(15)
text.Value = "Game is starting"
print("Game starting")
wait(5)

local plrs = game.Players:GetChildren()
for i = 1,#plrs do
    local num = math.random(1,8)
    plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
end

text.Value = "Welcome to Chances Of Doom..."
wait(5)
text.Value = "Let's begin."
wait(5)
text.Value = "A random player will be eliminated..."
print("A random player will be eliminated...")
wait(10)
game.Workspace.LightBulb.Lightbulb.Light.Color = Color3.new(170, 0, 0)

print(#plrs)
if #plrs >= 1 then
    local eliminated = plrs[math.random(1,#plrs)]
    eliminated.Character.Humanoid.Health = 0 
    wait(3)
    text.Value = eliminated.Name.." has been eliminated."
end
game.Workspace.LightBulb.Lightbulb.Light.Color = Color3.new(255, 255, 255)
wait(5)
text.Value = "Anyways, let's move on..."
wait(5)
print("hi.....")
while true do
if #pL:GetChildren() <= 1 then
    print("Game restarting...")
    text.Value = "Restarting game..."
    wait(5)
    break
    end

    print("YEYEYEYY")
    wait(1)


end
end
end

0
I really wish people would preview their post before posting. ScuffedAI 435 — 4y
0
me too TheRealPotatoChips 793 — 4y
0
Please use the little blue button on the top to format your code. It's really hard to read and thus makes it hard for us to help you. Sensei_Developer 298 — 4y
0
oh Hederlunden 19 — 4y
View all comments (4 more)
0
I fixed it now... Sorry about that... Hederlunden 19 — 4y
0
you could've edited your post rather than creating a new answer. Mayk728 855 — 4y
0
didn't know you could do that... Hederlunden 19 — 4y
0
Ok, now I have changed the whole thing... Please help me... Hederlunden 19 — 4y

1 answer

Log in to vote
0
Answered by
Mayk728 855 Moderation Voter
4 years ago
Edited 4 years ago

you can't have two while loops running simultaneously. one loop should be enough to do the job.

EDIT: fixed player checking

local text = game:GetService('ReplicatedStorage'):WaitForChild('TextValue')
local Players = game:GetService('Players')
local PlayersLeft

local IsPlayingFolder = Instance.new('Folder')
IsPlayingFolder.Parent = game:GetService('ServerStorage')

function DoubleCheck()
    local pl = #Players:GetChildren()
    if pl <= 1 then
        return true
    else
        return false
    end
end

workspace.BackGroundMusic:Play()
while wait(1) do
    PlayersLeft = #Players:GetChildren()
    if PlayersLeft <= 1 then
        text.Value = "Not enough players..."
        print("Not enough players")
    elseif PlayersLeft > 1 then
        text.Value = "Intermission"
        print("Intermission")

        wait(15)

        if DoubleCheck(PlayersLeft) == true then
            text.Value = "Not enough players..."
            --double check if everyone's there
            return
        end

        text.Value = "Game is starting"
        print("Game is starting")

        wait(5)

        for i,v in pairs(Players:GetChildren()) do
            local newValue = Instance.new("BoolValue")
            --value type doesn't matter, just the name
            newValue.Name = v.Name
            newValue.Parent = IsPlayingFolder
        end

        local plrplaying = IsPlayingFolder:GetChildren()
        for i,v in pairs(plrplaying) do
            local num = math.random(1,8)
            local plr = Players:FindFirstChild(v.Name)
            if plr and plr.Character and plr.Character:FindFirstChild("Head") then
               plr.Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
            end
        end

        text.Value = "Welcome to Chances of Doom..."
        wait(5)
        text.Value = "Let's begin."
        wait(5)
        text.Value = "A random player will be eliminated..."
        print("a rando will be elim")
        wait(10)
        workspace.Lightbulb.Lightbulb.Light.Color = Color3.new(170,0,0)

        PlayersLeft = Players:GetChildren()
        print("Players left: "..#PlayersLeft)
        if #PlayersLeft > 1 then
            local eliminated = PlayersLeft[math.random(1,#PlayersLeft)]
            if eliminated.Character and eliminated.Character:FindFirstChild('Humanoid') then
                eliminated.Character.Humanoid.Health = 0
            end
            if IsPlayingFolder:FindFirstChild(eliminated.Name) then
                IsPlayingFolder[eliminated.Name]:Destroy()
                --removes their in game presence after killing
            end
            wait(3)
            text.Value = eliminated.Name.." has been eliminated."
            wait(5)
        elseif #PlayersLeft == 1 then
            print('Only one person in-game!')
            return
        end

        workspace.LightBulb.Lightbulb.Light.Color = Color3.new(255, 255, 255)
        wait(5)
        text.Value = "Anyway, let's move on..."
        wait(5)
        local PlayersLeftPlaying = IsPlayingFolder:GetChildren()
        if #PlayersLeftPlaying == 1 then
            text.Value = PlayersLeftPlaying[1].Name.." is the last man standing! Restarting..."
            wait(5)
            for i,v in pairs(IsPlayingFolder:GetChildren()) do
                v:Destroy()
                --make sure no values are left
            end
            return
        end
        --moving on?
    end
end
0
Thank you so much! Btw, do you know a way I can print the name of the player who won? Hederlunden 19 — 4y
0
when the round starts, you'd have to create a value for each player showing that they're in the game. once they're eliminated, remove the value. eventually, if only one person has a value, they're the winner. Mayk728 855 — 4y
0
Hmm... I'm kinda new to scripting so can you show an example of that? Hederlunden 19 — 4y
0
i've edited the script. if it works, please upvote. i spent 40 min on this lol. Mayk728 855 — 4y
View all comments (13 more)
0
21:11:24.988 - ServerScriptService.Test:51: attempt to index nil with 'Character' Hederlunden 19 — 4y
0
:/ I'm sorry... Hederlunden 19 — 4y
0
that's my bad! i just did another edit, go ahead and try again. :) Mayk728 855 — 4y
0
everything was going great but... this is probably the last bug... 21:32:35.281 - ServerScriptService.Test:90: attempt to concatenate nil with string Hederlunden 19 — 4y
0
made an edit to line 90, try again o.O and i apologize for the errors, i can't test this script each time so things are bound to happen. Mayk728 855 — 4y
0
ServerScriptService.Test:90: attempt to concatenate boolean with string im sorry Hederlunden 19 — 4y
0
LOL my bad, i should be sorry here. made another adjustment to line 90. if there's still an error, you can add me on Roblox and i can continue further testing with you. Mayk728 855 — 4y
1
do you know why it never gets to print("four?") return end print("four?") --moving on? end end Hederlunden 19 — 4y
0
not sure what that's supposed to mean? Mayk728 855 — 4y
1
its in the end of the script but I added a print("four") to see how long the script would go. But it never got to that and after a player wins, I want to repeat it (i will add more later)) Hederlunden 19 — 4y
0
it's because at line 96, i added "return" which restarts the while loop. you can remove it and it should print what you wanted after. Mayk728 855 — 4y
1
Dude.. Thank you so much for all of this! If I could upvote I would but I don't have enough reputation. :(. But I will definetly remember this and upvote when I can! Hederlunden 19 — 4y
0
np!! :D Mayk728 855 — 4y
Ad

Answer this question