Gui keeps duplicating

Hello Devs,
I have this special script that when you creating a game it clones a gui and it is visible to all players but i have this issue where it clones more than one Ui and it is not visible to all players in the game I need help to fix this issue and code. Below is the video and the code.

in serverscriptservice

local CreateGameRemotes = game.ReplicatedStorage.CreateGameRemotes
local JGR = game.ReplicatedStorage.JoinGameRemotes
local CreateGame = CreateGameRemotes.CreateGame
local Theme = CreateGameRemotes.ThemeEvent
local PlrCount = CreateGameRemotes.NoPlrs
local Duration = CreateGameRemotes.DurationEvent
local Template = script.Parent

local Plrs = {}

CreateGame.OnServerEvent:Connect(function(plr, theme, totalplrs)
    local TempClone = Template:Clone()
    TempClone.Name = plr.Name
    table.insert(Plrs, plr)
    TempClone.PlayerName.Text = plr.DisplayName.."'s Room"
    TempClone.Theme.Text = theme
    for _, player in pairs(game.Players:GetPlayers()) do
        if TempClone.Parent ~= player.PlayerGui.TitleScreen.ServerGameList.Gamelist then
            TempClone.Parent = player.PlayerGui.TitleScreen.ServerGameList.Gamelist
            end
    end
end)

in local script in starterGui the last create button

CGCreate.MouseButton1Click:Connect(function()
    CGRemotes.CreateGame:FireServer(CGCreate.Theme.Value, CGCreate.PlrCount.Value)
    CGRemotes.ThemeEvent:FireServer(CGCreate.Theme.Value)
    CGRemotes.NoPlrs:FireServer(CGCreate.PlrCount.Value)
    CGRemotes.DurationEvent:FireServer(CGCreate.AnsDuration.Value)
    CreateGame.Visible = false
    ServerGameList.Visible = true
end)

in the local script to create and move to the game list is the

CGRemotes.CreateGame:FireServer(CGCreate.Theme.Value, CGCreate.PlrCount.Value)

the explorer

by the way the script is in the template

all help is appreciated!

It’s only visible for one player because you only created one clone.

Could you show me the “Template” Variable? i know how to fix the duplicating

oh how do I give it to all players properly?

Change that function to this.

CreateGame.OnServerEvent:Connect(function(plr, theme, totalplrs)
    local TempClone = Template:Clone()
    TempClone.Name = plr.Name
    table.insert(Plrs, plr)
    TempClone.PlayerName.Text = plr.DisplayName.."'s Room"
    TempClone.Theme.Text = theme
    for _, player in pairs(game.Players:GetPlayers()) do
        TempClone:Clone().Parent = player.PlayerGui.TitleScreen.ServerGameList.Gamelist
        -- clones it into their player gui
    end
end)

here it is just on top of the Plrs table

for i, v in pairs(Template:GetChildren()) do
		if v:IsA("ViewportFrame") then -- change Viewportframe to whatever Frame ur using (i guess ur just using a frame)
			v:Destroy()
		end
	end

Add this after end), should stop the cloning

Thanks the code does work it shows it to all players but the duplicating problem is not fixed though

well the template duplication isnt in the template it self but in the Game list
image
so therefore it is not working

CreateGame.OnServerEvent:Connect(function(plr, theme, totalplrs)
    local TempClone = Template:Clone()
    TempClone.Name = plr.Name
    table.insert(Plrs, plr)
    TempClone.PlayerName.Text = plr.DisplayName.."'s Room"
    TempClone.Theme.Text = theme
    for _, player in pairs(game.Players:GetPlayers()) do
        if not player.PlayerGui.TitleScreen.ServerGamelist:FindFirstChild(plr.Name) then
            TempClone:Clone().Parent = player.PlayerGui.TitleScreen.ServerGameList.Gamelist
        end
    end
end)

Redo whole script, you’ve done it in the wrong way. When you create the game, you should have this game inside of a table on the server and send a remote call to client to create the gui for it. But that’s not the most reliable way and there are many problems. I would make a data model inside of a ReplicatedStorage which represents all created games.

oh well i will redo and create the data model and thanks for the advice

honestly, for all the year i’ve been scripting in roblox, i’ve putten a gui to player’s interface from server only once. it was when i was working with soft shutdown screens and loading screens

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.