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

Why is my Last Player Alive script not working?

Asked by 8 years ago

So basically I am trying to make a script that finds the last player(s) alive. By using StringValues but it seems not to work I don't know why.

`  game.Players.PlayerAdded:connect(function(player)
    player.CharacterAdded:connect(function(character)
            Instance.new("StringValue", player)
    end)
    end)


function Alive()
    local player = game.Players:GetChildren()
    local D = player:findFirstChild("StringValue").Value == "Dead"
    local A = player:findFirstChild("StringValue").Value == "Alive"
    wait(1)-- game time
    if A == true then
        local Message = Instance.new("Message", workspace)
        Message.Text = (A.Name.."is the last player left!")
    end
end

Alive() `

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
8 years ago

Well, you only called the function once. If you want to check who is the last alive, you'll need to either have an event fired, or a while loop. In my script, I'll use a while loop. Secondly, in your script, it doesn't detect if that is the ONLY person alive. It just checks if they are alive, even though others could be. So:

game.Players.PlayerAdded:connect(function(p)
    p.CharacterAdded:connect(function(character()
        Instance.new("StringValue", p) -- This line is going to insert a value into the player, whenever they spawn. I either recommend having this in the PlayerAdded function by itself, or having a separate script remove it once they die, which would be tiresome. #MyFirstSuggestion
    end)
end)

-- I recommend having the following in a separate script. I myself haven't tested this, but I'm not sure if a while loop after an event is a good idea. I apologize if I'm wrong!

while wait() do
    alive = 0
    Last_Plr = nil
    for i,v in pairs(game.Players:GetPlayers()) do
        if v:findFirstChild("StringValue").Value == "Alive" then
            alive = alive + 1
            Last_Plr = v
        end
    end
    if alive == 1 then
        local Message = Instance.new("Message", workspace)
        Message.Text = Last_Plr.Name.." is the last player left!"
    end
end
0
It still does not work I fixed the errors in it but I can't seem to get it to work :( CorruptBullet 5 — 8y
0
What is the error? @CorruptBullet Shawnyg 4330 — 8y
0
Sorry this is late but their is no error I have been working on it for days trying to reverse engineer it but still can't get it to work. @Shawnyg CorruptBullet 5 — 8y
0
@CorruptBullet Try setting up print() around the script. See where it stops Shawnyg 4330 — 8y
0
@Shawnyg I got it to work instead of a String value I used a IntValue and changed it up a bit and got it to work. Thanks anyways CorruptBullet 5 — 8y
Ad

Answer this question