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!
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
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!