hi, im trying to make it so if a second player joins, it will go into an intermission state. but its not really working when i tested it defaulting to the not enough player text instead. is there anything wrong with my code?
players = game.Players:GetChildren()
timer = game.ReplicatedStorage.Value.Value
playa = game.Players
playa.PlayerAdded:Connect(function()
if #players > 1 then
print("intermission")
repeat
timer -= 1
until timer == 0
else
print("not enough players")
end
end)
There’s a few issues with this system. First, it does intermission every time a player joins, so if you have 2 players and another joins, it’ll enter the intermission loop again (unless you use debounce but I still wouldn’t recommend using PlayerAdded to handle that). Instead I would have a thread that constantly runs on a heartbeat loop, checking if the round can start. And like @notsad2ALT said, when you define players in the beginning, it gets all the players who are currently in the game and stores that into a new table, it can’t update itself and since this is at the very beginning of a server script it’ll ALWAYS be empty (server loads before any client does). So inside your intermission loop, you need to reassign players to game.Players:GetChildren() (or :GetPlayers()). Here’s a little sample I wrote that uses a thread and heartbeat. The doRound function yields the main thread and when it returns (as the round ends), the game will smoothly go back into the intermission loop and check that there are enough players to start the round.
local playerService = game:GetService("Players") -- personal preference i just like having this as a variable
local runService = game:GetService("RunService") -- for heartbeat instead of while task.wait()
local players = {}
--local timer = game.ReplicatedStorage.Value.Value you don't really need timer value since you can honestly just use attributes instead
local function doRound()
task.wait(5)
warn("round end")
return
end
task.spawn(function()
while true do
players = playerService:GetPlayers() -- also personal preference, :GetChildren() does work here but :GetPlayers() is better
if #players>1 then
task.wait(15) -- intermission time
doRound()
end
runService.Heartbeat:Wait()
end
end)
playerService.PlayerAdded:Connect(function()
-- Not sure what you need this for now so I just left it
end)
The doRound function is supposed to handle the round and stop the intermission until the round ends, so if you wanted lets say a ‘last player alive’ type round, you would have it be something like
function doRound()
local alive = playerService:GetPlayers()
local lastAlive:Player
while true and #alive>1 do
runService.Heartbeat:Wait()
alive = playerService:GetPlayers()
end
lastAlive = alive[1]
warn(`{lastAlive.Name} won this round, ending round and returning to intermission`)
end
You don’t have to use that function and I probably wouldn’t, I’d use module scripts but it was just for example purposes
Usually when doing this you do (or atleast thats what I do)
:GetPlayers()
And each time a player joins it resets and does it again I would do this personally
local players = game.Players:GetPlayers()
local timer = game.ReplicatedStorage.Value.Value
local playersService = game:GetService("Players")
local intermissionTime = 10 --whatever number you want fr
while true do
if #players > 1 then
print("intermission")
for i = intermissionTime, 0, -1 do
timer = i
task.wait(1)
end
else
print("Need more players")
end
task.wait(0.2)
end
Hopefully this works I wrote all of this out on here so there may be typos
I also added some customizable time for intermission