As you can see on Player 2’s screen it only shows the text label with their username on it
While on Player 1’s screen it shows both text labels.
local script:
local Players = game:GetService("Players")
local PlayerNameText = script.Parent.PlayerNameText
local myServerName
local InServer = false
local PlayerName = ""
game:GetService("ReplicatedStorage").JoinServers.JoinServerPlayer.OnClientEvent:Connect(function()
InServer = true
print(InServer)
end)
game:GetService("ReplicatedStorage").ServerNames.OnClientEvent:Connect(function(player, ServerName)
-- Invokes in ServerIDHandler
print(ServerName .. "PLAYERS SCRIPT")
myServerName = ServerName
if InServer == true then
local playerTextClone = PlayerNameText:Clone()
playerTextClone.Text = PlayerName
playerTextClone.Parent = script.Parent
playerTextClone.Name = PlayerName
end
end)
game.ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function(plr)
local label = PlayerNameText
label.Text = plr.Name
label.Parent = script.Parent
end)
game.ReplicatedStorage.PLRNames.OnClientEvent:Connect(function(plrname)
task.wait(0.1)
if InServer == true then
print(tostring(plrname))
local playerTextClone = PlayerNameText:Clone()
playerTextClone.Text = tostring(plrname)
playerTextClone.Parent = script.Parent
playerTextClone.Name = plrname
PlayerName = plrname
InServer = false
else
print("Player is not in server")
end
end)
game.ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(player)
if Players.LocalPlayer.Name == myServerName then
print("PlayerNameText is not getting destroyed because the player is the server creator")
else
local PlayerTextClone = script.Parent:FindFirstChild(Players.LocalPlayer.Name)
PlayerTextClone:Destroy()
end
end)
It fires PLRNames to all clients and in my local script it calls that remote
ServerScript
ReplicatedStorage.JoinServers.JoinServerServer.OnServerEvent:Connect(function(player)
ReplicatedStorage.PLRNames:FireAllClients(player.Name)
local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
if MaxPlayerValue then
MaxPlayerValue.Value = MaxPlayerValue.Value + 1
if MaxPlayerValue.Value >5 then
print("more than five players")
end
end
end)
Also, what do you mean, make the local script accessible for the player?
Also If you would like to see the two server scripts, here they are:
MaxPlayers:
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local MaxPlayersFolder = ServerScriptService.MaxPlayers.MaxPlayersFolder
ReplicatedStorage.MaxPlayers.OnServerInvoke = function(player)
local MaxPlayersValue = Instance.new("NumberValue")
MaxPlayersValue.Parent = MaxPlayersFolder
MaxPlayersValue.Value = 1
MaxPlayersValue.Name = player.Name .. " 's Server"
print(player)
if MaxPlayersValue then
return true
else
return false
end
end
ReplicatedStorage.JoinServers.JoinServerServer.OnServerEvent:Connect(function(player)
ReplicatedStorage.PLRNames:FireAllClients(player.Name)
local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
if MaxPlayerValue then
MaxPlayerValue.Value = MaxPlayerValue.Value + 1
if MaxPlayerValue.Value >5 then
print("more than five players")
end
end
end)
ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
local MaxPlayerValue = MaxPlayersFolder:FindFirstChild(player.Name .. " 's Server")
if MaxPlayerValue then
MaxPlayerValue.Value = MaxPlayerValue.Value - 1
end
end)local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local MaxPlayersFolder = ServerScriptService.MaxPlayers.MaxPlayersFolder
ServerIDHandler:
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerIDHandler = ServerScriptService:FindFirstChild("ServerIDHandler") or Instance.new("Folder", ServerScriptService)
ServerIDHandler.Name = "ServerIDHandler"
local ListofIds = ServerIDHandler:FindFirstChild("ListofIds") or Instance.new("Folder", ServerIDHandler)
ListofIds.Name = "ListofIds"
ReplicatedStorage.CreateServers.CreateServer.OnServerInvoke = function(player)
print("CreateServer")
local ServerID = tostring(math.random(10000, 99999))
local ServerEntry = Instance.new("StringValue")
ServerEntry.Value = ServerID
ServerEntry.Name = player.Name .. " 's Server"
ServerEntry.Parent = ListofIds
local ServerName = Instance.new("StringValue")
ServerName.Name = player.Name
ServerName.Value = player.Name.. "'s SERVER"
ServerName.Parent = ServerScriptService.ServerIDHandler.ServerNames
-- set the parent as the last property because it's more optimized
for _, ReceivePlayer in ipairs(game.Players:GetPlayers()) do
game:GetService("ReplicatedStorage").ServerNames:FireClient(ReceivePlayer, player.Name, ServerName.Name)
print("for loop")
end
player.AncestryChanged:Connect(function()
if not player.Parent then
local playerServerID = ListofIds:FindFirstChild(player.Name .. " 's Server")
if playerServerID then
playerServerID:Destroy()
end
end
end)
return ServerID
end
ReplicatedStorage.JoinServers.JoinServer.OnServerInvoke = function(player, ServerID)
for _, server in ipairs(ListofIds:GetChildren()) do
if server.Value == ServerID then
print(player.Name .. " successfully joined server with ID:", ServerID)
ReplicatedStorage.JoinServers.JoinServerPlayer:FireClient(player)
return true
end
end
warn(player.Name .. " failed to join server with ID:", ServerID)
return false
end
ReplicatedStorage.CreateServers.CreateServerServer.OnServerEvent:Connect(function(player)
ReplicatedStorage.CreateServers.CreateServerPlayer:FireAllClients(player)
end)
ReplicatedStorage.CloseFunction.CloseServerUI.OnServerEvent:Connect(function(player)
ReplicatedStorage.CloseFunction.LeaveServer:FireAllClients(player)
end)
Join = It fires when a player enters the right ServerID ( a randomised 5 number code)
Creating Server = Remote event (in the local script it is called CreateServerPlayer) fires when a player creates the server
Just in the server script fire it name to all clients and also update the a frame in startergui to where the players show up then if an new player joins they get the frame from startergui
Also I don’t recommend to add random people to your game as they can do things to your game or steal your stuff I once worked with a team then somoane of that team removed my stuff
Make sure to update what you do on the clients also in starter Gui if it’s isn’t updating there new joined players won’t see it they get what’s in startergui on join not what other clients have
How would I save the data (the text labels and the text) to the server which then it can fire a remote event to the clients in the ui server? (lets call it ui server instead of server to not get confused)