Problem with Bindable event

  • 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

this is not what I needed, my problem is that the gui is copied several times to the making players

Oh, i mabe fix that, i give answer when try it

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.

Yep, take your time and don’t forget to update me as well.

1 Like

Nope, thats not working, idk, but it clone at next player

So the template just clones itself to a new player?

yes, thats it. Template clone to new player

https://www.veed.io/view/4124e261-9c87-4919-ba41-9a2bcbf8d5f2?panel=share

Try this:

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)

So, how is it going?
If my reply helped, consider solutioning it so the topic will close

This is not work :frowning: i think me need to use remote event, but i need data store service on it

where is the handler script located

aw, nvm, im just re-script this.

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