How do i clone gui only once for each player in game

So im trying to get every players name and clone it in gui showing their name, but this script clones the same players name every 20 seconds and so im not sure how to make it so it clones only one time per player

local players = game:GetService("Players")
local RP = game:GetService("ReplicatedStorage")
local templates = RP:WaitForChild("Templates")
local tradingTemplate = templates:WaitForChild("TradingFrame")

while wait() do
for i, player in pairs(players:GetPlayers()) do
	local newTemplate = tradingTemplate:Clone()
	newTemplate.Parent = script.Parent
	newTemplate:WaitForChild("playerName").Text = player.Name
	end
	wait(20)
end


2 Likes
local players = game:GetService("Players")
local RP = game:GetService("ReplicatedStorage")
local templates = RP:WaitForChild("Templates")
local tradingTemplate = templates:WaitForChild("TradingFrame")

for i, player in pairs(players:GetPlayers()) do
	local newTemplate = tradingTemplate:Clone()
	newTemplate.Parent = script.Parent
	newTemplate:WaitForChild("playerName").Text = player.Name
	end
	wait(20)
end

this maybe?

2 Likes
local players = game:GetService("Players")
local RP = game:GetService("ReplicatedStorage")
local templates = RP:WaitForChild("Templates")
local tradingTemplate = templates:WaitForChild("TradingFrame")

players.PlayerAdded:Connect(function(player)
    local newTemplate = tradingTemplate:Clone()
    newTemplate:WaitForChild("playerName").Text = player.Name
    newTemplate.Parent = script.Parent
end)

this would run whenever a player joins the game

just do it onPlayerAdded

game.Players.PlayerAdded:Connect(function(player)
  local newTemplate = blablabla
end)

that wouldn’t work

only way we could use a i, v loop is adding an if statement checking if the player hasn’t joined yet

if we don’t do that then it will clone multiple times for that player

but it’s on client

wouldn’t that only run once?

I am not sure if you want to clone it once when the player joins. Or maybe you are trying to replicate it to the player every 20 seconds. Clarify it.

To detect if the player has the GUI on their folder, FindFirstChild may be useful for you.

local players = game:GetService("Players")
local RP = game:GetService("ReplicatedStorage")
local templates = RP:WaitForChild("Templates")
local tradingTemplate = templates:WaitForChild("TradingFrame")

while wait() do
    for i, player in pairs(players:GetPlayers()) do
        if not player:FindFirstChild("TradingFrame") then
            local newTemplate = tradingTemplate:Clone()
            newTemplate.Parent = player
            newTemplate:WaitForChild("playerName").Text = player.Name
        end
    end
    wait(20)
end

ServerScriptService is where the script should be stored.

do it on the server

PlayerGui is for each player and doing it on the server won’t change the text for everyone, it will be different and each name will be shown

A question, is the parent of the script only going to contain these trading templates? If so, I recommend moving the script outside of it, making a variable that references it and using :ClearAllChildren() on it before adding the templates again if you’re doing a refresh feature thing

you can get PlayerGui using a PlayerAdded function which is FAR better then a loop in this situation

It’s on the client? I’m so confused why are you handling other players things from the client. Only handle each player on their respective machine. (beside the fact it’s impossible to replicate or change things on other clients from one client)

-- local script
local newtemplate = blablabla

exactly use the server instead of the client

so i parent it to the playergui on server? But i thought the server wasn’t suppose to handle the gui stuff

StarterGui is cloned into each player’s PlayerGui, this lets the server mess with gui safely

with this logic changing StarterGui in game will only effect people who join since they would clone the changes that got made

It’s not which is why you have 2 options

game.Players.PlayerAdded:Connect(function(player)
     local PlayerGui = player:WaitForChild("PlayerGui") -- parent ScreenGui's to here
end)

or

-- local script
local newTemplate = blablabladowhateveryouwant

The second one is better because, there really is no reason for the server to be involved.

I made a whole thread about this, the results are you shouldn’t handle “client things” on the server

i don’t understand the 2nd one