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

How would I load something only when a player joins?

So here's an example of what I made

local p = game.Players.NumPlayers

while true do
if p > 0 then
--This is where my code is
end
wait()
end

But instead of going through an entire loop it stops when the player count is at 0 and never checks if it changed.

2 answers

game.Players.PlayerAdded:connect(function(player) -- Detect the player joining, and fire function with the player instance
-- Your code here
end

Here's the solution: update the variable in the while loop otherwise it will stay at 0 because that's what it was assigned.

while wait() do
	--Put your "p" in the while loop
	local p = game.Players.NumPlayers
	
	if p > 0 then
		print("--This is where my code is")
	end
end

Or keep it outside

local p = game.Players.NumPlayers

while wait() do
	p = game.Players.NumPlayers
	
	if p > 0 then
		print("--This is where my code is")
	end
end

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