I need help with my script that I made. My script should enable to see all the player’s names in a gui listed under each other.
However there is an issue. When I run a server test with 3 players, It only shows the name of player 1 and player 3 in the gui instead of player 1, player 2 and player 3. I have done research related to this topic but could not find any helpful information.
local mainFrame = script.Parent:WaitForChild("MainFrame")
local sample = script.Parent.MainFrame:WaitForChild("Sample")
local function addPlayers()
local players = game.Players:GetPlayers() --getting the players in game
for i, v in pairs(players) do --looping to the players and adding a textbutton with the players name.
local clonedSample = sample:Clone()
clonedSample.Name = v.Name
clonedSample.Text = v.Name
clonedSample.Visible = true
clonedSample.Parent = mainFrame
table.remove(players,i) -- Removing the player from the table, so it does not add unlimited buttons with the player's name. I think this is the problem.
end
end
local function update()
for _, v in pairs(mainFrame:GetChildren()) do --removing the old buttons
if v.Name ~= "Sample" and v.Name ~= "PlayersText" and v.Name ~="UIListLayout" then
v:Destroy()
print("Old frames destroyed")
end
end
end
while true do wait()
wait(4)
addPlayers()
wait(4)
update()
end
Alright, you could make it so whenever the server detects a new player has been added to the server it adds a new button with the players name to the button.
Yeah, that’s right. You would need to make a function that makes the servers add a label with the player’s name on joining. Make a UiLayout so I stays in line and nothing gets placed on a random position.
Creating a player list is actually more simple than it seems. Here are 3 things to know:
When you join, loop through all the players and create a frame for each one
When someone else joins, use a PlayerAdded event to add a player to the list
When a player leaves, use the PlayerRemoving event to remove a player from the player list
--// Local Script //--
local Players = game:GetService("Players")
local function Create(player)
end
local function Remove(player)
end
for i, player in pairs(Players:GetPlayers()) do
Create(player)
end
Players.PlayerAdded:Connect(function(player)
Create(player)
end)
Players.PlayerRemoving:Connect(function(player)
Remove(player)
end)
Are there any errors in your output because this script looks like it should work.
The only thing that looks questionable is your table.remove part of your script. I would also use a playeradded event and update you GUI every time a player joins or leaves. Then table.remove wouldn’t be necessary.
Made this script. No errors are given. Nothing is happening. I dont think you can use Players.PlayerAdded:Connect(function(player) in a local script. Heres the script
local mainFrame = script.Parent:WaitForChild("MainFrame")
local sample = script.Parent.MainFrame:WaitForChild("Sample")
local players = game:GetService("Players")
for i, v in pairs(players:GetPlayers()) do
local clonedSample = sample:Clone()
clonedSample.Name = v.Name
clonedSample.Text = v.Name
clonedSample.Parent = mainFrame
end
players.PlayerAdded:Connect(function(player)
local clonedSample = sample:Clone()
clonedSample.Name = player.Name
clonedSample.Text = player.Name
clonedSample.Parent = mainFrame
end)
I agree that you cannot use playeradded in a localscript. You need to do it in the server and pass over the player who joined to a localscript with a remote event.
Would you mind help me a little bit with the remote event part. I’m not really good with remote events. How do I make it once a player joins (in a server script) a remote event gets fired to a local script with the name of the player
Alright. I can’t really help you any more because I have said what I have to say. I would really take a playeradded approach to this because it is good practice to not have while true do loops running all the time. Good luck @InvalidArgvmen!
Hey, I’m back sorry for delay I was making a post.
PlayerAdded and a PlayerRemoved;
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function()
-- Adding joined players.
end)
Players.PlayerRemoving:Connect(function()
--Removing the players.
end)