I'm trying to make a minigame game and the way I want it to work is by putting all players into a table and then removing them when they die or leave. Currently I'm working on the die part, but it's not working. Everything else seems to work just fine on its own.
local maps = game.ServerStorage.maps:GetChildren() local storage = game.Workspace.place local clock = 30 --game starts quickly local Players = game:GetService("Players") local plrs = {} function mapchange() print ("bruh") storage:ClearAllChildren() --clear map wait(10) -- intermission local random_map = maps[math.random(1,#maps)] random_map:Clone().Parent = storage -- new map loaded wait(1) for i,v in pairs(game.Workspace:GetChildren()) do if v:findFirstChild("Humanoid") then local rx = math.random(-20,20) local rz = math.random(-20,20) if v:findFirstChild("HumanoidRootPart") then v.HumanoidRootPart.Position = Vector3.new(rx, 79.5, rz) --tp all players inside end end end for i,v in pairs(game:GetService("Players"):GetPlayers()) do table.insert(plrs, v) --living players all added to list end print(#plrs) --number of living players --THIS PART BELOW WONT WORK for i,v in pairs(Players:GetChildren()) do v.Character.Humanoid.Died:Connect(function() print("git rekt" .. v.Character.Name) --this prints correctly local deadguy = v for i, v in pairs(plrs) do if v == deadguy then table.remove(plrs, i) end end end) --if everyone finna die but one person (or not one person) --this also works when I test with one player if #plrs == 1 then print("one left") mapchange() print (table.concat(plrs)) plrs = {} elseif #plrs == 0 then mapchange() end end end --2 minutes per round, this timer makes sure of that while true do if clock ~= 0 then wait(1) clock = clock - 1 if clock == 0 then clock = 120 wait(1) mapchange() end end end
I figured it out, or at least I figured out that something else is broken. I just added the if statement about one players inside the whole function and it worked. However, don't copy this script because it breaks for another reason. The part I was asking about wasn't broken.
The problem I see here is that you named the two variables in the second for
loop the same as the two variables in the first for
loop. Try changing the "i, v" in the second for loop to different variable names, otherwise it will mix things up.
so the second for
loop should be this
for a, b in pairs(plrs) do
instead of this
for i, v in pairs(plrs) do