What do you want to achieve?
I want to achieve a function that returns a table of players that are in a specific team.
What is the issue?
The issue being, that my function iterates through every player 20 times… That is definitely not supposed to happen.
For some reason, this function is returning a player 20 times?
function getPlayersWithTeam(team)
local list = {}
for i, v in pairs(plrs:GetPlayers()) do
if v.Team == team then
print(v.Name) -- x20 :/
table.insert(list, v)
end
end
return list
end
I can’t see anything wrong with this code above… Please let me know if I did something horribly wrong, or ask me to show another section of my scripts. This is really annoying because it makes me feel stuck at a point, and I can’t make progress because there’s a problem. (lol)
I don’t see the error on my side.
Things To Do/ Things You May Be Doing Wrong:
Add a Wait() before the return list.
You may be doing getPlayersWithTeam over and over (Like if you are using a Loop, such as While true do or for i,v in pairs())
I hope I helped!
Hope you can get it fixed!
Ok, first of all try by printing the table (using ipairs and not pairs, plrs:GetPlayers() return a array, not a dictionarry):
for i,v in ipairs(game.Players:GetPlayers()) do
print(i, v)
end
See if you have 20 times the same player. If yes, then you can understand why. But, do you have 20 Players in the Players Service or not? If not, then you have a problem with your whole code i think. Idk, i am asking.
I think you should use breakpoints, with these you can easy find out your problem. Just try, breakpoints are basic debugging, so they are what you need.
I’m stupid, well apparently there was some other bit of code that called it 20 times, and printed it each time.
Common mistake, I guess. But now I wonder how I can work around this;
-- t_play is the "playing" team
local disasterCount = 20 -- How many disasters each round
local disasterSeconds = 15 -- How many seconds for each disaster
local checkplrteamsval = 4 -- Something that speeds up the check for last player standing etc.
local winners = {}
for i = 1, disasterCount, 1 do -- Start of disaster cycle
for j = 1, disasterSeconds * checkplrteamsval, 1 do -- Waiting counter...
local plrsWithTeam = getPlayersWithTeam(t_play)
if #plrsWithTeam <= 1 then -- Oh we have a winner, before the last cycle
for x, v in pairs(plrsWithTeam) do
table.insert(winners, v)
end
break -- Supposed to exit the disaster cycle
end
wait(1 / checkplrteamsval) -- Update quickly so people don't have to wait 15 seconds for next round.
end
-- do the disasters.....
end
-- Add other people on the list if the disaster cycle ended before there was a last man standing.
Printing is not the issue here, I mistakenly made the code so it called the getPlayersWithTeam function 20 times, it was 20 because of disasterCount. Thanks for trying to help. I still wonder how I can workaround this.
(i looked it up) and it said, yes it would break out of all loops.
in full wording:
So yes the entire loop would break . You should simply not do anything if there isn’t a character. All functions in Lua have a return explicit or implicit. As magnalite said return will immediately stop the execution of the function and the values are passed back to where the function was called from.
That would work better than my function, but the problem still stands, a section of code far below that function calls it 20 times, so it’s not really a solution, but thanks for pointing out that Team:GetPlayers() exists!
break only terminates the current loop. If it’s a nested loop, it’ll terminate only the nested loop early (if used in the nested loop).
You can use continue (added with the new Luau features), which skips the current iteration of a loop; meaning you could have a loop iterate 10 times, and skip #2 by using something like if i == 2 then continue end
Sometimes loops aren’t the answer, though. Functions and Events can yield better results sometimes.
If you want here is a new „sort of break“ called continue. For more informations you should see the official website of Luau. https://roblox.github.io/luau/syntax.html