Function is printing players 20 times

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? :confused:

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

image

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)

3 Likes

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!

Where is the line if code that calls that function? I don’t see snything wrong in the function itself

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.

is it when you touch the item? or no? (if its in a part)

edit: oh wait, nevermind its not haha

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.
1 Like

Try doing

			print(v.Name) * 1

idk if that will work but maybe…

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.

2 Likes

oh, ok… ill try and look at it a little more and see if i can figure something out.

as i dont know much i prob wont find it. :grimacing:

I have a question:

  • Does break break out of all loops, or just one? If only one, it would be great if Roblox added break(n) or break: n, which breaks out of x loops.

A workaround is putting this disaster cycle in a function and returning instead.

(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.

Then, what could be wrong here?

Hey, do you think that the Team:GetPlayers() function would work for you?

If you did t_play:GetPlayers(), it should have the same effect as your function.
(It returns an array of all players in the team)

1 Like

i have no idea, a few minutes ago i thought maybe an else or an elseif statement might help in some way but now i dont think so at all…

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

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.

Oh yeah, I haven’t noticed that one.

why dont you just use team:GetPlayers() ?

Someone else mentioned that in the thread, thanks for pointing that out though.

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