[HELP] How would i want to tag a player with a script?

Ok guys i wanted to ask you guys this because i havent advanced at this point im not asking for a script just a example on how i would tag a player if they were to survive.

For example Survive The Natural DIsaster.

1 Like

CollectionService is definitely the best way to handle this.

1 Like

Thanks exactly what I needed I’ll learn how to use this then I’ll publish my game!

1 Like
local e = game.Players:GetChildren()
for i,v in pairs(e) do
  if v.Value.Value==true then
    print(v.Name)
  end
end

Games like Survive the Natural Disaster typically use an array with association to determine who has survived. In that regard as well, rather than trying to find people who survived, it keeps a list of all alive players and removes them from the table when they die. The array at the end of the round determines who stayed alive for the round.

With the advent of CollectionService, this makes things far more easier, as SebastianAurum posted. You can give a tag to the characters of all those who are alive and remove them as they die. I say characters since removal of the tag would be automatic, but players who die within 5 seconds of an ending round can also win. You’ll have to handle that case yourself by checking health.

local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")

local ALIVE_TAG_NAME = "PlayerSurvivor"

-- On round start
for _, player in pairs(Players:GetPlayers()) do
    if not (player.Character == nil) then
        CollectionService:AddTag(player.Character, ALIVE_TAG_NAME)
    end
end

-- On round end
local winnerPlayers = {}

for _, taggedCharacter in pairs(CollectionService:GetTagged(ALIVE_TAG_NAME)) do
    local player = Players:GetPlayerFromCharacter(taggedCharacter)
    if player and taggedCharacter:FindFirstChild("Humanoid") then
        -- Putting this on a new line for readability's sake
        if taggedCharacter.Humanoid.Health > 0 then
            table.insert(winnerPlayers, player)
        end
    end
end

Because of the necessity of confirming if a player is dead when the round ends to prevent dead player corpses from being counted as survivors, we have to set up another table that contains the players that the characters are attached to.

We can then do whatever we want once we have the list of players gathered.

4 Likes

Thanks ill try it out when i load into the studio.

Would i use this as a module since it is using a table?

No, not necessarily. How you integrate this into your code is up to you.

No ill just put it with my script lol thanks dude imma credit you in my game too