Hello! I’m working on a phone system, and currently I’m working on a messaging app.
I want to check every second for the players that are in the server, and make a button for every user in the server. Currently, it’s only making a button for yourself. The code might be messy, and it’s a serverscript, just because I couldn’t figure out how to get the rest of my scripts to work if I wouldn’t do it this way.
Anyways here’s the script:
while true do
for i,v in pairs(Players:GetChildren()) do
local convoList = v.PlayerGui:WaitForChild("PhoneGui").PhoneScreenFrame.DisplayFrame.Apps.Messages.ConvoList
if not v.PlayerGui:WaitForChild("PhoneGui").PhoneScreenFrame.DisplayFrame.Apps.Messages.ConvoList:FindFirstChild(v.Name) then
local sampleButton = remotes.GUISamples.SampleButton
local newButton = sampleButton:Clone()
print(v.Name, v.UserId)
local thumbType = Enum.ThumbnailType.HeadShot
local thumbSize = Enum.ThumbnailSize.Size420x420
local content, isReady = Players:GetUserThumbnailAsync(v.UserId, thumbType, thumbSize)
local newButton = sampleButton:Clone()
newButton.Visible = true
newButton.ConvoName.Text = v.Name
newButton.Name = v.Name
newButton.Parent = v.PlayerGui.PhoneGui.PhoneScreenFrame.DisplayFrame.Apps.Messages.ConvoList
newButton.ProfilePicture.Image = content
for _, button in pairs(convoList:GetChildren()) do
if button:IsA("UICorner") then
print("dont delete")
elseif button.Name == "UIListLayout" then
print("dont delete")
else
print("delete")
button:Destroy()
end
end
else
return
end
end
wait(1)
end
This is what it looked like when my friend tried it, and there was 2 people in the server.
The solution is to not extremely inefficient hard to maintain while loops.
Events are for this exact purpose
This is how it’s done
local function onPlayerAdded ()
-- code that will run for each player
end
-- if the code above yields this is necessary
for i,player in ipairs(game.Players:GetPlayers())do
task.spawn(onPlayerAdded)(player)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
You are constantly checking and updating something in a busy while loop (very inefficient)
I recommend using GetPlayers (not GetChildren) at the beginning, as well as PlayerAdded events. I don’t think PlayerAdded fires for people who arrive before the script is called.