Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I load something only when a player joins?

Asked by 6 years ago

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

Log in to vote
0
Answered by 6 years ago
game.Players.PlayerAdded:connect(function(player) -- Detect the player joining, and fire function with the player instance
-- Your code here
end
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

Answer this question