So I’m creating a story game, but the problem is I’ve tried searching up everywhere on how to transfer information between each place.
Example!
If three people join a game, once the first person loads in, I want a 30 second counter to wait for the other 2. If the other 2 don’t join in that time, they basically get booted. If they do, then the game starts once they’ve all loaded. But how do I get the game to know how many people have joined? It could be 2, 3, 4. Any of those numbers. Because if only 2 people have joined and the game is waiting for 3 people, well that’s a waste of time. I would need the specific number of how many people actually joined the waiting queue.
If anyone could help me, that would be much appreciated!!
Hello, so I just wrote this piece of code that will do precisely what you mentioned in the post.
This is a local script and you can place it in StarterPlayerScripts
local numberofplayers = #game.Players:GetPlayers()
repeat task.wait() until numberofplayers >= 1
task.wait(30) --Countdown Time
if game:IsLoaded() == false then
script.Parent.Parent:Kick() --Kicks the player that hasn't loaded
else
script:Destroy()
end
I would do it server sided, using PlayerAdded event. Adding the players to a table to count how many players are in the server. Doing a while loop with a counter, max time to wait 30 seconds.
The loop breaks if enough players joined game (3 or more added into the table), calling a function to actually start the game.
If you wanna count how many players are in that specific server, why you wanna send information between servers/places? Or you want to know if theres enough players in different servers?
local CurrentPlayers = {} -- Table of players that joined
local TimeToWait = 30 -- Max time to wait before kicking players
local GameWaiting = false -- Debouce for the while loop to only happens once
-- Function to Start your game
local function StartGame()
warn("Start game with", #CurrentPlayers, "players")
-- DO STUFF
end
-- PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
table.insert(CurrentPlayers, player) -- insert joined player into the table
if not GameWaiting then
GameWaiting = true
task.spawn(function()
while true do
if #CurrentPlayers >= 3 then
task.spawn(function()
StartGame()
end)
break
end
if TimeToWait <= 0 and #CurrentPlayers < 3 then
warn("Kick players, time to wait is over")
for _, p in pairs(game.Players:GetPlayers()) do
p:Kick("Too much time waiting for more players")
end
end
TimeToWait -= 1
warn("Time left before kicking:", TimeToWait)
task.wait(1)
end
end)
end
end)