I recently asked a question about a bad argument error when choosing a random player. Now, I want a way to implement a gui into the rest of the players.
Go to: to see this previous question.
This is the current code that has to be updated with this feature:
local playerstorage = game:GetService("Players") local storage = game:GetService("ServerStorage") local guifolder = storage.Guis local players = {} wait (40) for _, player in pairs(game.Players:GetPlayers()) do if player and player.Character then local humanoid = player.Character:WaitForChild("Humanoid") if humanoid and humanoid.Health > 0 then table.insert(players, player) end end end if #players >= 2 then local alien = players[math.random(1, #players)] local alieng = guifolder.Alien local alieng2 = alieng:Clone() alieng2.Parent = alien.PlayerGui alien = script --More code else print '2 players needed!' end wait(0.1)
You can use table.remove
when choosing alien
so that it returns the removed value, while at the same time nullifying it from the table players
.
Therefore, whenever you iterate through players
later on in your code, it will returned everyone but the removed value, alien.
local playerstorage = game:GetService("Players") local storage = game:GetService("ServerStorage") local guifolder = storage.Guis local players = {} wait (40) for _, player in pairs(game.Players:GetPlayers()) do if player and player.Character then local humanoid = player.Character:WaitForChild("Humanoid") if humanoid and humanoid.Health > 0 then table.insert(players, player) end end end if #players >= 2 then -- remove random value from table local alien = table.remove(players, math.random(#players) local alieng = guifolder.Alien local alieng2 = alieng:Clone() alieng2.Parent = alien.PlayerGui for _, player in pairs(players) do print(player) -- alien was removed, so this will return -- the list of all values inside of table `players` -- except alien. end --More code else print '2 players needed!' end
Also, in your first generic for loop, you are iterating through the table returned by players:GetPlayers
, which only returns player objects.. therefore, you do not need to check if they have a character, because that's a given.
for _, player in pairs(game.Players:GetPlayers()) do local humanoid = player.Character:WaitForChild("Humanoid") if humanoid and humanoid.Health > 0 then table.insert(players, player) end end
It seems as if all you need is one more loop that just ignores the alien.
for i, v in pairs(game.Players:GetChildren()) do if v.PlayerGui:findFirstChild("Alien") == nil then -- Add Gui end end