Why does running this function inside my for i,v loop stop the loop?

Just as the title says, the loop works until it hits the function, where it stops completely. Any way to keep this loop to running for other players whilst still sending all players through the function?

function Give(Player)
	local Clone = GUI:Clone()
	local TopBox = Player:WaitForChild("PlayerGui"):FindFirstChild("TopBox")
	if TopBox == nil and script:GetAttribute("MinigameRunning") == false then
		Clone.Parent = Player.PlayerGui
		Prepare()
	end
end

function respawn()
	script:SetAttribute("MinigameRunning", false)
	for i,v in pairs(game.Players:GetPlayers()) do
		print(v)
		local attr = v:GetAttribute("Score")
		if attr then
			v:SetAttribute("Score", nil)
			v:LoadCharacter()
		end
		local GUI1 = v:WaitForChild("PlayerGui"):FindFirstChild("BottomBox")
		local GUI2 = v:WaitForChild("PlayerGui"):FindFirstChild("Leaderboard")
		if GUI1 then
			GUI1:Destroy()
		end
		if GUI2 then
			GUI2:Destroy()
		end
		Give(v)
	end
	MN:Destroy()
	MN = nil
end

I’d assume its because you were returning in the Give function, but I don’t see a return there.

Whats the Prepare() function?

Well after the Prepare() is called, more functions are called from said function, but there is a return in Prepare(), but that’s only if there’s less than 2 players, which this loop has so far only stopped when I have done a 2 player test myself, so that return shouldn’t be called.

function Prepare()
	local Players = PlayerService:GetPlayers()
	if #Players < 2 then
		NotReady()
		return
	end
	Ready(Players)
end

Just to continue on this, even though the loop doesn’t run through every player, the script still continues on as normal, just affecting 1 player.

I don’t see anything wrong with the idea of a loop calling a function.

Here is a quick test I put together:

Screen Shot 2024-07-13 at 4.43.39 PM

I also tried making Give() a local function too before writing this, so that couldn’t be an issue…

How do you “know” the loop is stopping?

What are you wanting to happen that is not happening?

There’s video evidence in my 2nd reply that even though there are 2 players in the server, this loop that connects to all players only affects the one on the right side. Additionally, the “print (v)” line that starts the loop only prints 1 player, telling me that the function just stops the loop. What should happen is what happens to player on the right.

Maybe I am confused; shouldn’t the prepare() function run before any other script since it is waiting for enough players?

And then the respawn() function should run every time a player is added?

The framework goes like this
When a player first joins a server, there’s a playeradded event connected to Give() to give a GUI
Then Prepare() is called from Give() to check playercount, and if theres not enough then NotReady()
When another player joins, this player goes through those same functions, and if there is enough (which there should be,) it runs through Ready()

When the minigame is over, respawn() is called to bring all playing players back to spawn, and Give() is called to give the GUI back as I removed it when the minigame began. I thought this would create a good gameplay loop to keep checking for players and keep running the minigames, but I guess it might not.

I decided to make a different function that would due to same thing as Give() except for calling Prepare(). This worked fine. So, I guess I just have to somehow call Prepare() a different way?

1 Like

I ended up making a new function that could be called from both first joining players and restarting the minigame cycle, and it works perfectly! Thanks for your time everyone.
(Also found out that LoadCharcter erases the PlayerGUI anyway so there was no need for those final lines)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.