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())]
end
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
end
end
found = false
plate.Parent = workspace.plates
plr.Character:MoveTo(plate.Position)
loop_times = loop_times - 1
end
PlayersInRound = game:GetService("Players"):GetPlayers()
end
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)
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
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
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.