Find random child function timing out

I have no idea why this is happening. I just recently asked a question about something else I was having an issue with, but that was fixed. Now I run into this issue,

for some reason whenever I run a random child function a second time, it freezes then times out.

Here is the function:

function frc(model)
	return model:GetChildren()[math.random(1,#model:GetChildren())]

Why does this time out a second time? I can not figure it out.

Code where function is called:

function begin()
	local players = game:GetService("Players"):GetChildren()
	local loop_times = #players
	while loop_times > 0 do
		local plate = frc(game:GetService("ReplicatedStorage").plates)
		local found = false
		while found == false do
			plr = frc(game:GetService("Players"))
			if plr.plate.Value == false then
				plr.plate.Value = true
				plate.Name = plr.Name.."'s plate"
				found = true
		found = false
		plate.Parent = workspace.plates
		loop_times = loop_times - 1
	PlayersInRound = game:GetService("Players"):GetPlayers()

My recommendation would just separate it into more lines. It will simplify it and any errors would be more visible.

function frc(model)
      local allmodel = model:GetChildren()
      local Item = allmodel[math.random(1, #allmodel)]
      return Item
1 Like

How large is the model? More children means there’s more for the table to allocate, which may cause slowdowns.

Other then that, I don’t see any visible errors, it is basically the same way you would find a Map for a mini-game, etc. It should work flawlessly.

It is a folder containing 16 8x1x8 simple parts in replicatedstorage

Can we see which function is calling frc? It’d also be nice to know what the timeout message is (if there is one.)

Then the problem isn’t this function, the problem is whatever’s calling it is freezing.

Still getting the same error, this is it:

20:02:31.697 - ServerScriptService.gameScript:53: Game script timeout
20:02:31.698 - Stack Begin
20:02:31.698 - Script 'ServerScriptService.gameScript', Line 16 - global begin
20:02:31.699 - Script 'ServerScriptService.gameScript', Line 53
20:02:31.699 - Stack End

Line 53 in gamescript is where the function begin is being called, and line 16 is where the function “begin” starts. (function called to begin the game)

Can you post the actual script??? The output doesn’t really say anything unless you know the script.

Just added the full begin function to the original post.

Just added the full “begin” function to the original post.

Well, for one, you aint cloning the plates, so that will cause it to break eventually.

At the end of the game, the plates are placed back into the folder. I’ll try cloning them anyways, just incase something between me moving them out and moving them back in is breaking something

Because it is grabbing the part from there then not replacing it, so its running out of plates very quickly,

How sure are you that found will be true eventually?

EDIT: Meant to reply to @MrSprinkleToes

That explains everything, I was overlooking it, You are setting a value to false and trying to run a loop if that is true, It aint having a chance to set it to true.

OH! I was forgetting to change a value called “plate” to false when they died, therefore that part of the script thought they were still in a game even though they weren’t, therefore breaking

1 Like

Remember to mark a solution!

Happy your problem is solved :smiley:

I will be marking a solution, I want to make sure I am right.

1 Like

While you seem to have found why your infinite loop occurs, I think this code still has some obvious design problems; there doesn’t need to be a potentially-infinite loop in the first place. Assigning a random object to each player doesn’t require rejection sampling, it can be done in O(N) time. If you have 100 players, by the time you get to assigning the 100th player a plate, the algorithm you have could loop many, many times randomly checking the other 99 players unnecessarily and repeatedly, until it eventually randomly draws the one remaining plateless guy. This is because game:GetService(“Players”):GetChildren() is always everyone in game. You should make a copy of the list of players who do not have plates, and remove each player from the list as they get assigned plates.

Making random pairings also requires only one random number draw per pair; you don’t need to randomly pick a plate, and then randomly pick a player. You can iterate through one of these lists (players or plates) and randomly pick just the other. The pool being randomly sampled shrinks. It’s equivalent to pre-shuffle one of the lists and then pair them off in a for loop where player[i] gets plate[i].

Implemented efficiently, there should be only one loop that iterates a finite number of times, therefore no possibility for infinite loop freezing at all.