Hello, I’m making a game type system, and I have a script that increases the value of “CurrentPlayers” when someone joins. However, for some reason, my PlayerAdded function isn’t working. Is there something wrong with my code?
My code:
local Players = game:GetService("Players")
local currentPlayers = Instance.new("NumberValue")
currentPlayers.Name = "CurrentPlayers"
currentPlayers.Parent = game.ReplicatedStorage
currentPlayers.Value = 0
GameCanBeStarted = Instance.new("StringValue")
GameCanBeStarted.Parent = game.ReplicatedStorage
GameCanBeStarted.Name = "GameCanBeStarted"
GameCanBeStarted.Value = "false"
local Gui = script.Parent
Gui.Enabled = true
Players.PlayerAdded:Connect(function()
currentPlayers.Value += 1
print("Someone joined, so this server now has", currentPlayers.Value, "player(s)!")
if currentPlayers.Value >= 2 then
if Gui.Enabled == true then
Gui.Enabled = false
GameCanBeStarted.Value = "true"
else
GameCanBeStarted.Value = "false"
end
end
end)
Players.PlayerRemoving:Connect(function()
currentPlayers.Value -= 1
print("Someone left, so this server now has", currentPlayers.Value, "player(s).")
if currentPlayers.Value >= 2 then
if Gui.Enabled == true then
Gui.Enabled = false
GameCanBeStarted.Value = "true"
else
GameCanBeStarted.Value = "false"
end
end
end)
Is this a server script or a client script? If it’s in the server, why is there a ScreenGui and why are you enabling it?
Make sure your server script is somewhere like ServerScriptService and is enabled. Try using this pattern to account for players that are already in game just in case it connects the signal late:
local function onPlayerAdded(player)
end
for _, player in Players:GetPlayers() do
task.spawn(onPlayerAdded, player)
end
Players.PlayerAdded:Connect(onPlayerAdded)
there is a screen gui because if there are less than 2 players in the server there will be a gui on the screen that says you can either invite friends or wait
From the looks of this script, it looks as if it is a Localscript. The PlayerAdded event will not return when the Localplayer is added, and will only detect when other players are added. Personally, I believe you should make this a Server script so that you can detect all players that join.
//Edit//
To add to this reply, you can instead use #Players:GetPlayers() to return an amount of players, meaning you won’t need that "CurrentPlayers" value that you have.
Once again, assuming that your script is a localscript, it will not work in ServerScriptService. Local scripts only work locally (as the name suggests). Consider switching it to a Server script, and it should work.
Is this in a local or server script? if its a server script and you want a gui to display when there is less than two players, you would need some sort of remote event to fire all clients.
Can you explain what type of script this is and where its placed?
i guess i changed it or something but i just recently checked the script is inside the screen gui called GuiForOnePlayer, not in serverscriptservice. Sorry about that!
I guess i made a typing mistake but when i checked my script it wasnt inside of serverscriptservice, sorry for the false info. Also, it is a basic script, not a local script
Ohh, that makes more sense. Assuming the script is a descendant of StarterGui, the reason the function isn’t working is because PlayerGuis are not added until after a player has joined, meaning you would only be able to detect when other players join. I suggest modifying the script so that it edits everyone’s Gui at once. After that, put this script in ServerScriptService and let me know how it goes!
Revised code:
local Players = game:GetService("Players")
local currentPlayers = Instance.new("NumberValue")
currentPlayers.Name = "CurrentPlayers"
currentPlayers.Parent = game.ReplicatedStorage
currentPlayers.Value = 0
GameCanBeStarted = Instance.new("StringValue")
GameCanBeStarted.Parent = game.ReplicatedStorage
GameCanBeStarted.Name = "GameCanBeStarted"
GameCanBeStarted.Value = "false"
Players.PlayerAdded:Connect(function()
currentPlayers.Value += 1
print("Someone joined, so this server now has", currentPlayers.Value, "player(s)!")
task.wait(2)
for i,plr in pairs(game.Players:GetPlayers()) do
local Gui = plr.PlayerGui:WaitForChild("YourGuiNameHere") -- Change to your Gui's name
Gui.Enabled = true
if currentPlayers.Value >= 2 then
if Gui.Enabled == true then
Gui.Enabled = false
GameCanBeStarted.Value = "true"
else
GameCanBeStarted.Value = "false"
end
end
end
end)
Players.PlayerRemoving:Connect(function()
currentPlayers.Value -= 1
print("Someone left, so this server now has", currentPlayers.Value, "player(s).")
for i,plr in pairs(game.Players:GetPlayers()) do
local Gui = plr.PlayerGui:WaitForChild("YourGuiNameHere") -- Change to your Gui's name
Gui.Enabled = true
if currentPlayers.Value >= 2 then
if Gui.Enabled == true then
Gui.Enabled = false
GameCanBeStarted.Value = "true"
else
GameCanBeStarted.Value = "false"
end
end
end
end)