All was fine until I had to check if the players had a certain team to be put into a table. So now it checks if the team is right and then it insert the player into the team.
However, for some odd reason the right players do not get added into the table and the prints in the for i,v loop are showing that everything is going correct. Furthermore, let’s say we have 4 players in a game. Later when I print all the players in the table it is usually the first 2 players that are in it when it is supposed to be Player3 and Player4. I have no idea what is the reason behind this.
I searched through all the possible outcomes, the wiki, this forum but no one seems to know the answer.
Here is the script;
function Survivors()
Survivors = {}
for i, v in pairs (game.Players:GetPlayers()) do
if v and v.Character then
print(v.Name)
if v.TeamColor == game.Teams.Survivors.TeamColor or v.TeamColor ==
game.Teams.Infected.TeamColor then
print(v.Name)
local Humanoid = v.Character:FindFirstChild("Humanoid")
if Humanoid and Humanoid.Health > 0 then
if v.Character:FindFirstChild("Torso") then
table.insert(Survivors, v)
end
end
end
end
end
end
In addition, that is a function that is run in a game later.
Help is well appreciated
Is the problem that every time the function is called Survivors is set as {} at the beginning of the function, as a local variable?
I’m not really sure what else it could be, as there isn’t much context here - but that would mean the table is reassigned each time the function is called, and is only stored locally. Try setting it globally outside the function if you’re calling it multiple times
Note:
Would recommend naming the table and the function slightly differently as a good programming technique. Having them named the same could cause problems and it can be confusing to read, too!
Maybe the problem lies elsewhere in your code, but here is how I would go about getting the survivors:
local teams = game:GetService("Teams") -- get the teams service once at the beginning of the script
local survivors = {}
local function getSurvivors()
survivors = {}
for _,plr in next, game.Players:GetPlayers() do -- loop through all players
if plr.Character and plr.Character:FindFirstChild("Humanoid") and plr.Character.Humanoid.Health > 0 and ((plr.Team == teams.Survivors) or (plr.Team == teams.Infected)) then -- check if they are on the right team and are alive
table.insert(survivors, plr) -- add the player to the table
end
end
end
Could you perhaps provide other parts of code or try finding out where the table changes to different values than expected??
If it isn’t interference from something else then my guess is that you have some different teams with the same TeamColor as the ones you’re checking. Player has a Team property, so you can check that instead of comparing colors.
additionally, Team has a GetPlayers method, so you can be a bit more direct by using that.
You’re looking for a randomized placement, I presume.
Here’s something else
local Survivors = {}
local function getSurvivors()
for i, v in next, (game.Players:GetPlayers()) do
if v and v.Character and (v.TeamColor == game:GetService("Teams").Survivors.TeamColor or v.TeamColor == game:GetService("Teams").Infected.TeamColor) and v.Character:FindFirstChild(“Humanoid”) and v.Character:FindFirstChild(“Humanoid”).Health > 0 then
table.insert(Survivors, v)
end
end
end
local function clearSurvivors()
-- clear the table and perhaps the teams
Survivors = {}
end
On the side, you could use some random algorithms to scramble the order.
You can completely avoid iterating through all players and running this logic by instead using GetPlayers of the Survivors team (the method 1waffle1 mentioned). Don’t reinvent the wheel while a method is already available for your usage. I dare say this even comes across as an XY Problem.
local Teams = game:GetService("Teams")
local SurvivorsTeam = Teams.Survivors
local Survivors = SurvivorsTeam:GetPlayers()
-- If you *really* need a table:
local Teams = game:GetService("Teams")
local SurvivorsTeam = Teams.Survivors
local Survivors = {} do
for n, Player in pairs(SurvivorsTeam:GetPlayers()) do
table.insert(Survivors, Player)
end
end
-- You can also convert into a function if you wish, though there's literally
-- no point in doing so. Use the first example.
local Teams = game:GetService("Teams")
local SurvivorsTeam = Teams.Survivors
local Survivors = {}
local function getSurvivors()
for n, Player in pairs(SurvivorsTeam:GetPlayers()) do
table.insert(Survivors, Player)
end
end
In the future, as well, please put your code in code blocks when posting to the forum. This makes it easier for us to read and interact with your code.
As said, maybe I did not explain as well, that there is no mistakes with the teams. I have used your method which is the same as the one I am using just the Team:GetPlayers() is different.
local function getSurvivors()
SurvivorsTable = {}
local players = game.Players:GetChildren()
for n, Player in pairs(SurvivorsTeam:GetPlayers()) do
table.insert(SurvivorsTable, Player)
print(Player.Name)
end
for i = 1, #SurvivorsTable do
print(players[i].Name)
end
end
Now let’s say there are 4 players and player3 and 4 are survivors, the rest of the 2 players should not be in the game.
Now the output for this is;
Player 3
Player 4
then when I asked for the script to circle all the players in the table I get;
Player 1
Player 2
Unfortunately, I was not as clear. What I meant was that random players are ending up in the table even though they are not supposed to be. Sorry for the miscommunication.
Thing is it does not use any other table functions in the script. This is the only part of the script that has any table function. It is supposed to reset the table at the start of every round.
NOW what I noticed is; let’s say 2 players are on the survivors team, then for some reason two players starting from the very first player gets picked but only 2. When there are 3 survivors that are supposed to be in the table, instead of them being in the table, the first 3 players even the players that are not in the Survivor team are in the table somehow. I do not know what is causing it and as said above.
when I do the table.insert and then print the player that goes into the table it gives all the right names but when I print out the table after it does inserting all the players it prints out the amount of players that shout be in the table but rather in a numerical order from all the players in the game.
I’m sorry, but I am wildly confused as to what you’re trying to do because your explanation is unclear. What are you trying to do? What is the problem? You need to be extremely explicit about your needs.
4 players? Player3 and 4 should be in a table but not Player1 and 2? Are both Player1 and 2 survivors then? Something about outputting Player3 and 4 and somehow getting 1 and 2?
Basically, player 1 and player 2 are NOT SURVIVORS. Player 3 and 4 ARE SURVIVORS. The loop that puts the players in the table is only supposed to put PLAYER 3 AND 4 ONLY. However, the amount of players that are survivors is the amount of players that go into the table, like said. Let’s play 5 players are survivors. THEN 5 players starting from the for i,v loop somehow get into the table even if they are not survivors.
Apparently this piece of code allowed infected team in the table. After the if statement, it added the infected too. That said, you should not add that to the if statement.
Unfortunately, I could not find a solution to this problem. Thus, I just used other methods.
I went over all players and checked if they are in the team later in the scripts without the use of tables. And to answer your question the infected are also supposed to be in the game. There are 3 teams: Survivors, Infected and Menu. The menu are the ones who are not supposed to be playing because they are, duh, in the menu. Anyhow, I guess I do not any help with the answer as I am using another option to solve my problem. However, for some odd reason I did not solve this and I do not know what is causing it. Thought, thank you for trying everyone Everyone who still may think there is a solution, I am happy to try it
You misintepreted my statement. The survivors’ table is filled with the infected players when running: if v.TeamColor == game.Teams.Survivors.TeamColor or v.TeamColor == game.Teams.Infected.TeamColor
Results in the table of: Survivors = {Survivor1, Survivor2, Infected1, Infected2}
Resolving this requires: if v.TeamColor == game:GetService("Teams").Survivors.TeamColor
For the infected, you’ll need another table. Else you could do a table like this:
Oh, I think I understand the problem now after reading the code again. You want to interact with everyone who is NOT on the Menu team, but only apply certain affects to survivors? Still not sure why you can’t only iterate over survivors but you’ve got two options:
Separate your statements.
if v.TeamColor == game.Teams.Survivors.TeamColor then
-- code
elseif v.TeamColor ==
game.Teams.Infected.TeamColor then
-- code
end
Just add another check inside the statement.
if v.TeamColor == game.Teams.Survivors.TeamColor or v.TeamColor ==
game.Teams.Infected.TeamColor then
print(v.Name)
local Humanoid = v.Character:FindFirstChild("Humanoid")
if Humanoid and Humanoid.Health > 0 and v.TeamColor == game.Teams.Survivors.TeamColor then
if v.Character:FindFirstChild("Torso") then
table.insert(Survivors, v)
end
end