Hello, I am making a game and if there are two players on the server then the countdown starts the problem is that only one player shows the countdown. I know perfectly well that this function works for one player but is it possible to make it work, script:
function PlayerAdded(plr)
local LobbyGui = plr.PlayerGui:WaitForChild("Lobby")
if #game.Players:GetChildren() == 2 then
for l=10,0,-1 do
LobbyGui.Info.Text = "Game start:".. l
wait(1)
end
end
end)
game.Players.PlayerAdded:Connect(PlayerAdded)
Try making a RemoteEvent when there 2 or more players in the server
Server:
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent -- add remoteevent and change path change path
function PlayerAdded()
if #game.Players:GetChildren() == 2 then
RemoteEvent:FireAllClients()
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
Local Script (inside the Gui):
-- add RemoteEvent path
local LobbyGui = script.Parent -- Change path
function GameStart()
for i = 10, 0, -1 do
LobbyGui.Info.Text = "Game start:"..i
wait(1)
end
end
RemoteEvent.OnClientEvent:Connect(GameStart)
However, I believe this could be bugging out since it always checks if there more than 2 players if someone joins just for your information.
local c = game:GetService("Run Service").Heartbeat:Connect(function()
if #game.Players:GetChildren() > 2 then
c:Disconnect
for l=10,0,-1 do
LobbyGui.Info.Text = "Game start:".. l
wait(1)
end
end
end)
You need to create a RemoteEvent inide of ReplicatedStorage. Try this:
Put this script inside of ServerScriptService:
local waitBeforeStart = 3 -- wait until begin
function PlayerAdded(plr)
local LobbyGui = plr.PlayerGui:WaitForChild("Lobby")
if game.Players:GetChildren() == 2 then
wait (waitBeforeStart) -- wait for players to load
game.ReplicatedStorage.GameStart:FireAllClients()
end
end)
game.Players.PlayerAdded:Connect(PlayerAdded)
Next put this script inside of StarterGui inside of the Text
local Text = script.Parent
game.ReplicatedStorage.GameStart.OnServerEvent:Connect(function()
for l=10,0,-1 do
Text.Text = "Game start:".. l
wait(1)
end
if Text.Text == "0" then
--add code here for when timer reaches 0
end
end)
Make sure the LocalScript is inside of the Text that shows the time in the LobbyGui
Also add a RemoteEvent inside of Replicated Storage and name it “GameStart”
You could just do this using a Local Script in StarterPlayerScripts
local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local PlayerGui = Player:WaitForChild("PlayerGui", 300)
local LobbyGui = PlayerGui and PlayerGui:WaitForChild("Lobby", 30)
function PlayerAdded()
if #PlayerService:GetChildren() >= 2 then
for l = 10, 0, -1 do
LobbyGui.Info.Text = "Game start:".. l
wait(1)
end
end
end
PlayerAdded()
PlayerService.PlayerAdded:Connect(PlayerAdded)