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() `
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