Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there something like string.split but with tables?

I am not requesting a script. I am requesting help I just need to know if there is a such thing. I am trying to split players into two teams but I got nothing.

1 answer

Here's something I came up with, you have to script this yourself though. I'm just giving you a plan of attack like you wanted.

You can make something like table.split() by just knowing how many contents are in a table. You can do this by doing #array / 2 Which will get you two equal parts of a the table.

Now we will round down the number to either math.ceil() or math.floor() , it's your choice to use which function. In this case I'm going to use math.floor()

You now can start assigning teams. Make sure you use a for loop for assigning the players to the first team and not an in pairs loop because you can control how much you can index the table of.

I gave you a starter template and not the full script to respect your choice of a full script:

local function assignTeams(team1, team2, players)
	local balanceTolerancy = math.floor(#players / 2)
	
	for i = 1, balanceTolerancy do
		-- player's indexed here will go to team1
	end
	
	for i,v in pairs(players)
		-- check if the player is on team1, if not then assign to team2.
	end
end

asignTeams(team1, team2, players)

Once you have finished assigning teams to player within a certain tolerancy, you can start assigning players to team2. You first need to check when looping if the player being looped by is not on team1, otherwise that could unbalance the game.

Now it's done. You can comment on this if you have any errors, I did not test my script before so beware of that.

This discussion is preserved as it was. New answers, comments, and votes are closed.