I call the function to load the gui interface, but if 2 people log in from the test, it will duplicate the other person’s
– // This is Handler
Events.loadInventory.Event:Connect(function(Player : Player, newTower : string, Status : string)
local PlayerGui = Player.PlayerGui
local pLobbyGui = PlayerGui.LobbyGui
local pMain = pLobbyGui.Menu
local pContainers = pMain.Containers
local pInventory = pContainers.Inventory
local Template = pInventory.Template:Clone()
Template.Parent = pInventory.Scroll
Template.Status.TextColor3 = Color3.new(0.529412, 1, 0.72549)
for _, v in towersInfo do
if v.Unit == newTower then
Template.Value.Value = v.Unit
Template.Status.Text = Status
Template.Tower.Text = v.Unit
Template.Visible = true
Template.Name = v.Unit
--print(Template.Value.Value)
Template.MouseButton1Click:Connect(function()
Inventory.Choosed.Value = v.Unit
if getTowerStatus(Player, v.Unit) == 'Owned' then
Inventory.Equip.Status.Text = 'Equip'
elseif getTowerStatus(Player, v.Unit) == 'Equipped' then
Inventory.Equip.Status.Text = 'Unequip'
end
end)
end
end
end)
– // This is reciver
PlayerService.PlayerAdded:Connect(function(Player)
spawn(function()
local playerData = Data[Player.UserId]
LoadData(Player) -- just load data
loadInventory(Player) -- doesn't matter
Events.LoadGui:Fire(Player) -- for fire
end)
end)
I added some sanity checks and modified your script a bit. Here it is:
-- handler
Events.loadInventory.Event:Connect(function(Player, newTower, Status)
local PlayerGui = Player:FindFirstChild("PlayerGui")
if not PlayerGui then return end
local pLobbyGui = PlayerGui:FindFirstChild("LobbyGui")
if not pLobbyGui then return end
local pMain = pLobbyGui:FindFirstChild("Menu")
if not pMain then return end
local pContainers = pMain:FindFirstChild("Containers")
if not pContainers then return end
local pInventory = pContainers:FindFirstChild("Inventory")
if not pInventory then return end
local Template = pInventory:FindFirstChild(newTower)
if not Template then
Template = pInventory.Template:Clone()
Template.Name = newTower
Template.Parent = pInventory.Scroll
end
Template.Status.TextColor3 = Color3.new(0.529412, 1, 0.72549)
for _, v in pairs(towersInfo) do
if v.Unit == newTower then
Template.Value.Value = v.Unit
Template.Status.Text = Status
Template.Tower.Text = v.Unit
Template.Visible = true
Template.MouseButton1Click:Connect(function()
Inventory.Choosed.Value = v.Unit
if getTowerStatus(Player, v.Unit) == 'Owned' then
Inventory.Equip.Status.Text = 'Equip'
elseif getTowerStatus(Player, v.Unit) == 'Equipped' then
Inventory.Equip.Status.Text = 'Unequip'
end
end)
end
end
end)
The receiver script will be the same.
Let me know if this helps
So you want to check if the template already exists, and if it does, instead of cloning it again, you want to just update it, that’s the thing with the duplication, you never checked if the template existed or not.
In my script, I’m checking it here
So if the template exists, the code will continue and skip this part and just update the values in the template, but if it does not exist, it will clone a new one and update the cloned one.
Events.loadInventory.Event:Connect(function(Player, newTower, Status)
local PlayerGui = Player:FindFirstChild("PlayerGui")
if not PlayerGui then return end
local pLobbyGui = PlayerGui:FindFirstChild("LobbyGui")
if not pLobbyGui then return end
local pMain = pLobbyGui:FindFirstChild("Menu")
if not pMain then return end
local pContainers = pMain:FindFirstChild("Containers")
if not pContainers then return end
local pInventory = pContainers:FindFirstChild("Inventory")
if not pInventory then return end
-- here, i'm using a unique identifier for the template based on the tower
local templateName = "Template_" .. newTower
local existingTemplate = pInventory:FindFirstChild(templateName)
if existingTemplate then
existingTemplate.Status.Text = Status
existingTemplate.Visible = true
else
local Template = pInventory.Template:Clone()
Template.Name = templateName
Template.Parent = pInventory.Scroll
Template.Status.TextColor3 = Color3.new(0.529412, 1, 0.72549)
Template.Status.Text = Status
Template.Tower.Text = newTower
Template.Visible = true
-- making sure that event connection is unique per template
Template.MouseButton1Click:Connect(function()
Inventory.Choosed.Value = newTower
if getTowerStatus(Player, newTower) == 'Owned' then
Inventory.Equip.Status.Text = 'Equip'
elseif getTowerStatus(Player, newTower) == 'Equipped' then
Inventory.Equip.Status.Text = 'Unequip'
end
end)
end
end)