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

How to find the last player still in the game?

Asked by
naturedat 124
3 years ago
Edited 3 years ago

This is what I got, I'm trying to make a battling game, so basically, I'm putting a tag into players who is battling when they are teleported in to the battle zone. So when someone died, the tag will be remove when they are spawned in the lobby so if who ever have the tag, it means that they are still in the game. the problem is that I'm trying to make a last survivor but I'm not sure how to count the number of players with tag.

local stat = game.ReplicatedStorage:WaitForChild("Stats")

for i, plar in pairs(game.Players:GetChildren())do
    if plar.Character then
        plar.Character.HumanoidRootPart.CFrame = spawns[1].CFrame
        table.remove(spawns,1)
        local gametag = Instance.new("BoolValue")
        gametag.Name = "GameTag"
        gametag.Parent = plar.Character -- Add tag to the players
    end
end

local gamecountdown = 20
repeat wait(1)
    stat.Value = "Time Left: "..gamecountdown
    local player = game.Players:GetChildren()
    for i = 1, #player do
        local v = player[i] -- making a table of players
        print(player)
        if v.Character:FindFirstChild("GameTag") then -- getting only the players with tag in the table
            -- See if there is only 1 player with a tag
            if #v == 1 then -- error: attempt to get length of an Instance Value
                local DataStore2 = require(1936396537) -- Datastore2 stuff
                local datagold = DataStore2("Golds", v)
                stat.Value = "Winner: "..v.Name
                datagold:Increment(5)
            break
                elseif #v == 0 then -- if the 2 last player kill each other at the same time
                stat.Value = "Everyone died..."
            break
            end
        end
    end
    gamecountdown = gamecountdown - 1
until gamecountdown == 0

-- The aftermath of the match, removing the tag from everyone to make sure that there's none
stat.Value = "Game Ended..."
for _, plr in pairs(game.Players:GetChildren())do
    if plr.Character then
        plr.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame
        if plr.Character:FindFirstChild("GameTag")then
            plr.Character:FindFirstChild("GameTag"):Destroy()
        end
    end
end

Ask me any question if there is a variable there that I forgot to include as this is only part of the script. Help please. Thank you!

0
You can easily just check if the players in the game is == 1, use a remote event and function that player. Grab the player client side because you can edit text on there, which I assume that's what you do in this case. If the player is the last standing then give them it. You're also making a game tag with a BoolValue, but not adding it to a surface gui, you're making it fine but you're not making Nicholas1088 169 — 3y
0
Why would i need to put it in the surface gui tho? since I'm putting the tag in the player's character and I'm trying to find the number of players with the tag. Because there is battle zone and a lobby zone, sorry if that sort of consufed you. naturedat 124 — 3y

2 answers

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

Hey, I noticed your code was bit messy. Here's something new that you can reference off of.

I tried to solve your issue, looks like you're doing it right, but one thing is for sure, you should be using Players:GetPlayers instead of Players:GetChildren

local DataStore2 = require(1936396537)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local stat = ReplicatedStorage:WaitForChild("Stats")
local gamecountdown = 20

for index,value in pairs(game:GetService("Players"):GetPlayers()) do
    pcall(function()
        value.Character["HumanoidRootPart"].CFrame = spawns[1].CFrame * CFrame.new(0,2.7,0)
        --[[
        I have added "* CFrame.new(0,2.7,0)" so that I can possibly help you
        prevent players from glitching in the ground.
        ]]
        table.remove(spawns,1)
        local gametag = Instance.new("BoolValue")
        gametag.Name = "gametag"
        gametag.Parent = value.Character
    end)
end

repeat
    wait(1)
    stat.Value = string.format("Time Left: %s",gamecountdown)
    for index,value in pairs(game:GetService("Players"):GetPlayers()) do
        pcall(function()
            if #game:GetService("Players"):GetPlayers() == 1 then
                local datagold = DataStore2("Golds",value)
                stat.Value = string.format("Winner: %s",value.DisplayName)
                datagold:Increment(5)
            elseif #game:GetService("Players"):GetPlayers() <= 0 then
                stat.Value = "Everyone died..."
            end
        end)
    end
    gamecountdown = gamecountdown - 1
until gamecountdown <= 0

stat.Value = "Game ended..."

for index,value in pairs(game:GetService("Players"):GetPlayers()) do
    pcall(function()
        value.Character["HumanoidRootPart"].CFrame = (workspace:FindFirstChild("SpawnLocation") or {["CFrame"] = CFrame.new(0,0,0)}).CFrame
        --[[
        Here's another suggestion:
        "(workspace:FindFirstChild("SpawnLocation") or {["CFrame"] = CFrame.new(0,0,0)})" is here so that you can set a manual teleportation if the spawn location isn't found, just set CFrame.new(0,0,0) to any coordinate.
        ]]
        for _,inst in pairs(value.Character:GetChildren()) do
            if string.lower(inst.Name) == "gametag" then
                pcall(inst.Destroy,inst)
            end
        end
    end)
end
0
Thank you for spending your time trying to fix my script, not many posters write that for me so thank you. By the way, can you also explain to me a bit about pcall? Because I have experience with it a little bit while making a data store but it would usually have a success and error thing which I'm not really sure of. Thank you! naturedat 124 — 3y
0
Well, there's some error on Line 25 and Line 29, actually there you are just calculating the number of players in the game and not the the number of player having the tag on them. Do fix that area BestCreativeBoy 1395 — 3y
0
There's no errors on those lines, GetPlayers returns a table of players, by using # before the table, you are getting the amount of values in the table. TheLuminent 1204 — 3y
Ad
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

There are few things that you have to add and edit in your code, which will be helpful in finding the player with tags.

-- First, in the starting, create an empty table 
-- Line 7
local TableWithTags = {}

-- Now, we will add the name of the player in the local table created
-- Line 10 
table.insert{TableWithTags, plar.Name}

-- Now, instead of looping with the player having tag, we will loop in the table created above
-- Adjust the lines of the looping of the tables accordingly
for a, b in ipairs(TableWithTags) do
    if a[2] == nil then -- Checks if no more than one player is left 
        -- YOUR CODE
    end

    if a[1] == nil then -- Checks if no one is left
        -- YOUR CODE
    end
end

-- If you want to remove the tag from the player when he/she dies
-- You have loop through the table
for i, v in ipairs(TableWithTags) do
    if TableWithTags[i] == plr.Name then -- Checks if the name of the player killed is in the table
        table.remove(TableWithTags, i) -- Removes the player from the table
    end
end

Lemme know if it helps!

0
It had helped a lot, I did thought of that while watching one of Alvin's video but he made it over complicated, I did used the table and so I tried using my table. But it didn't went well so I used your method instead. Thanks! naturedat 124 — 3y

Answer this question