Transferring data from a loop to a function?

Alright, so I’m messing up the player value (b) that is supposed to transfer individual players to a function. Think im completely off with the script itself, but it’s worth a try i guess. Anyone know how to fix it?

local cachedSpawns={}
		local playersPlaying={}
		
		function StartGame(b)
			local character = b.Character or b.CharacterAdded:wait()
			if character then
				StartGame()
				event:FireClient(b, {reason="HideGuis"})
				wait(.3)
				event:FireClient(b,{reason="CloseScreen"})
				wait(2.7)
				local randomSpawn = map.Spawns:GetChildren()[Random.new():NextInteger(1,#map.Spawns:GetChildren())]
				if cachedSpawns[randomSpawn] then
					repeat randomSpawn = map.Spawns:GetChildren()[Random.new():NextInteger(1,#map.Spawns:GetChildren())] wait() until not cachedSpawns[randomSpawn]
				end
				cachedSpawns[randomSpawn] = true
				character:WaitForChild('Humanoid').WalkSpeed = 0
				character:WaitForChild('Humanoid').JumpPower = 0
				character:SetPrimaryPartCFrame(randomSpawn.CFrame*CFrame.new(0,2.75,0))
				table.insert(playersPlaying,b.UserId)
				wait(2)
				event:FireClient(b,{reason="OpenScreen"})
				wait(0.7)
				--Countdown				
				event:FireClient(b,{reason="Countdown"})
				wait(5.25)
				event:FireClient(b,{reason="Audio",file="164486422"})
			end
		end	
		
		for a,b in ipairs(game.Players:GetPlayers()) do
			StartGame(b)
		end
1 Like

The player (b) returns nil in the function.

You’re calling StartGame() inside of the function, you don’t pass any parameters, so b is nil in the 2nd call.

Well, is there an alternative way to pass the parameters over from the loop to the function?

Change StartGame() to StartGame(b) inside the function?
Idk if thats what you want

Just remove that function call, I can’t see why it should be there. Because if it had a valid parameter (a player), it would recursively call forever and crash.

The actual loop call is correct.

no way… lmao, the StartGame() inside the function wasn’t intentional :joy: It was a typo from an earlier version of the script. Works now, thanks.