Hello, I’m new to the forum and was wondering if I should use a remote event to make sure that when a player clicks the play button on the menu and takes them to the team screen. The label on the team screen says waiting for player (2/8) and when they leave the label updates and says waiting for player (1/8).
Example: Player clicks button and joins the team screen
Waiting for players (2/8)
Player clicks to close the menu and it takes them off the team screen
Waiting for Players( 1/8)
The code I’m using for the remote event:
TeamPlayers(Local script) :
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamPlayers = ReplicatedStorage:WaitForChild("TeamPlayers")
local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild('PlayerGui')
local ScreenGui = PlayerGui:WaitForChild('ScreenGui')
local NPButton = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('NoviceFrame'):WaitForChild('NPButton')
local TeamScreen = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame')
local StatusLabel = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('StatusLabel')
local redFrame = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('redFrame')
local blueFrame = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('blueFrame')
TeamPlayers:FireServer()
TPEvent(Server Script):
local PlayerRequirement = 8
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeamPlayers = Instance.new("RemoteEvent", ReplicatedStorage)
TeamPlayers.Name = "TeamPlayers"
local Players = game:GetService('Players')
local NPButton = script.Parent.Parent.StarterGui.ScreenGui.mFrame.ArenaFrame.ArenaOFrame.NoviceFrame.NPButton
local StatusLabel = script.Parent.Parent.StarterGui.ScreenGui.mFrame.ArenaFrame.ArenaOFrame.TeamFrame.StatusLabel
NPButton.MouseButton1Click:Connect(function(player)
local function UpdateStatusLabel()
local NumPlayers = #Players:GetPlayers()
NumPlayers.Changed:Connect(function()
NumPlayers = #Players:GetPlayers()
end)
if NumPlayers < PlayerRequirement then
StatusLabel.Text = 'Waiting for players (' .. NumPlayers .. '/' .. PlayerRequirement .. ')'
else
wait(1)
StatusLabel.Text = 'Game Start!'
end
end
Players.PlayerAdded:Connect(UpdateStatusLabel)
Players.PlayerRemoving:Connect(UpdateStatusLabel)
UpdateStatusLabel()
end)
TeamPlayers.OnServerEvent:Connect(NPButton)
Is there a better way to do this besides a remote event?
Here are some picture for better visual:
I decided to make it more clear:
Would this code work to make it so that when a person clicks on the play button it would add them to the wait for players list. Then when they click the menu button it would remove them from the waiting for players list?
local PlayerRequirement = 8
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild('PlayerGui')
local ScreenGui = PlayerGui:WaitForChild('ScreenGui')
local NPButton = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('NoviceFrame'):WaitForChild('NPButton')
local TeamScreen = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame')
local StatusLabel = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('StatusLabel')
local redFrame = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('redFrame')
local blueFrame = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('blueFrame')
NPButton.MouseButton1Click:Connect(function(player)
local function UpdateStatusLabel()
local NumPlayers = #Players:GetPlayers()
if NumPlayers < PlayerRequirement then
StatusLabel.Text = 'Waiting for players (' .. NumPlayers .. '/' .. PlayerRequirement .. ')'
else
wait(1)
StatusLabel.Text = 'Game Start!'
end
end
Players.PlayerAdded:Connect(UpdateStatusLabel)
Players.PlayerRemoving:Connect(UpdateStatusLabel)
UpdateStatusLabel()
end)
1 Like
So you want to show how many Players (and who exactly) clicked Play in a Lobby and are waiting for a game to start?
1 Like
yes, that is exactly what I want
Then you can do this with RemoteEvents and RemoteFunctions.
Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local InLobby = {}
function playerJoined(Player)
-- The code you want to execute when a player joins
-- Add player to lobby
InLobby[Player.Name] = true
-- Notify all players
RemoteEvent:FireAllClients("playerJoined")
end
function playerLeft(Player)
-- Check if the player is in the lobby
if InLobby[Player.Name] then
-- The code you want to execute when a player leaves
-- Notify all players
RemoteEvent:FireAllClients("playerLeft")
end
end
RemoteEvent.OnClientEvent:Connect(function(Player, Type)
if Type == "join" then
playerJoined(Player)
elseif Type == "leave" then
playerLeft(Player)
end
end)
RemoteFunction.OnServerInvoke = function(Player, Type)
if Type == "requestPlayers" then
return InLobby
end
return nil
end
Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
-- Change script.Parent to your actual parent
local ScreenGui = script.Parent
local NPButton = ScreenGui:WaitForChild("mFrame"):WaitForChild("ArenaFrame"):WaitForChild("ArenaOFrame"):WaitForChild("NoviceFrame"):WaitForChild("NPButton")
local StatusLabel = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('StatusLabel')
local PlayerRequirement = 8
function UpdateStatusLabel()
local InLobby = RemoteFunction:InvokeServer("requestPlayers")
local NumPlayers = #InLobby
if NumPlayers < PlayerRequirement then
StatusLabel.Text = string.format("Waiting for players (%d/%d)", NumPlayers, PlayerRequirement)
else
wait(1)
StatusLabel.Text = "Game Start!")
end
end
RemoteEvent.OnClientEvent:Connect(function(Type)
if Type == "playerJoined" then
UpdateStatusLabel()
elseif Type == "playerLeft" then
UpdateStatusLabel()
end
end)
NPButton.MouseButton1Click:Connect(function()
-- Queue player up
RemoteEvent:FireServer("join")
end)
Please note that this is just an example and it may not fully work, and you will also need to write it differently depending on how your code is structured. If you need better explanation of a certain part in the code don’t hesitate to quote it!
where does the join and the leave come from?
The client sends it. join gets sent from the client when the player clicks the NPButton with the Left Mouse Button.
So it gives no errors but when I click the play button the Status Label simplys says “Label”.
Here is the code I put into NPButton. The TeamFrame is where the label is.
NPButton.MouseButton1Click:Connect(function()
local novice = script.Parent.Parent.Parent.TeamFrame
if novice.Visible == false then
novice.Visible = true
end
RemoteEvent:FireServer("join")
end)
I think that is what causing it? tell me if you want the whole thing instead
Does the RemoteEvent and RemoteFunction Class exist in ReplicatedStorage?
Does the Server receive any event? (You can check this by adding print(Player, Type) in the code)
I guess it doesn’t? this is where I put it and there was no print statement:
RemoteEvent.OnClientEvent:Connect(function(Player, Type)
if Type == "join" then
playerJoined(Player)
elseif Type == "leave" then
playerLeft(Player)
end
print(Player,Type)
end)
Is there anything in the output after 30 seconds, as example Warnings (orange text) or Errors (red text)?
Nothing after 30 seconds.
here is the serve script code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local InLobby = {}
function playerJoined(Player)
if playerJoined(Player) then
local welcomePlayer = Instance.new("TextLabel",ReplicatedStorage)
welcomePlayer.Text = "Welcome! "..Player.Name
end
InLobby[Player.Name] = true
RemoteEvent:FireAllClients("playerJoined")
end
function playerLeft(Player)
if InLobby[Player.Name] then
if playerLeft(Player) then
local playerthatLeft = Instance.new("TextLabel", ReplicatedStorage)
playerthatLeft.Text = Player.Name.." Has Left!"
end
RemoteEvent:FireAllClients("playerLeft")
end
end
RemoteEvent.OnClientEvent:Connect(function(Player, Type)
if Type == "join" then
playerJoined(Player)
elseif Type == "leave" then
playerLeft(Player)
end
print(Player,Type)
end)
RemoteFunction.OnServerInvoke = function(Player, Type)
if Type == "requestPlayers" then
return InLobby
end
return nil
end
here is the client script code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local Players = game:GetService('Players')
local LocalPlayer = Players.LocalPlayer
local ScreenGui = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.ScreenGui
--local ScreenGui = PlayerGui:WaitForChild('ScreenGui')
local NPButton = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('NoviceFrame'):WaitForChild('NPButton')
--local TeamScreen = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame')
local StatusLabel = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('StatusLabel')
--local redFrame = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('redFrame')
--local blueFrame = ScreenGui:WaitForChild('mFrame'):WaitForChild('ArenaFrame'):WaitForChild('ArenaOFrame'):WaitForChild('TeamFrame'):WaitForChild('blueFrame')
local PlayerRequirement = 8
local function UpdateStatusLabel()
local InLobby = RemoteFunction:InvokeServer("requestPlayers")
local NumPlayers = #InLobby
--local NumPlayers = #Players:GetPlayers()
if NumPlayers < PlayerRequirement then
StatusLabel.Text = string.format("Waiting for Players (%d/%d)", NumPlayers, PlayerRequirement)--'Waiting for players (' .. NumPlayers .. '/' .. PlayerRequirement .. ')'
else
wait(1)
StatusLabel.Text = 'Game Start!'
end
end
RemoteEvent.OnClientEvent:Connect(function(Type)
if Type == "playerJoined" then
UpdateStatusLabel()
elseif Type == "playerLeft" then
UpdateStatusLabel()
end
end)
NPButton.MouseButton1Click:Connect(function()
local novice = script.Parent.Parent.Parent.TeamFrame
if novice.Visible == false then
novice.Visible = true
end
RemoteEvent:FireServer("join")
end)
I should say that local script is in the NPButton itself:

Ah yes, you should not call the same function within a function. Try this in your serverside.
Server
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local RemoteFunction = ReplicatedStorage:WaitForChild("RemoteFunction")
local InLobby = {}
function playerJoined(Player)
local welcomePlayer = Instance.new("TextLabel",ReplicatedStorage)
welcomePlayer.Text = "Welcome! "..Player.Name
InLobby[Player.Name] = true
RemoteEvent:FireAllClients("playerJoined")
end
function playerLeft(Player)
if InLobby[Player.Name] then
local playerthatLeft = Instance.new("TextLabel", ReplicatedStorage)
playerthatLeft.Text = Player.Name.." Has Left!"
RemoteEvent:FireAllClients("playerLeft")
end
end
RemoteEvent.OnClientEvent:Connect(function(Player, Type)
if Type == "join" then
playerJoined(Player)
elseif Type == "leave" then
playerLeft(Player)
end
print(Player,Type)
end)
RemoteFunction.OnServerInvoke = function(Player, Type)
if Type == "requestPlayers" then
return InLobby
end
return nil
end
tried it and it is still not giving any errors or changing the label. Is it maybe something on the client side as well or where the scripts are located?
Question is, does it print now when the server receives a event?
oh sorry I just saw this (for some reason it did not give me a notification), No it does not.
Could you attach your current place file for the current code you have? At this point, I would like to look at the structure fully. If you do not want to, it’s fine, but it would help speed up the process.
local script (NQscript):

Server script:
