Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

how to make it where an instance is only created once if the same child name?

local Weapons = game.Players.LocalPlayer.Data:WaitForChild("Inventory").Weapons:GetChildren()
             number = 0
             children = #Weapons


            for  i = 1, #Weapons do

    local new = Instance.new("TextLabel")
                new.Parent = script.Parent
                new.Position = position + UDim2.new(0,0,0,30)
                position = position + UDim2.new(0,0,0,25)
                new.Size = UDim2.new(0,200,0,10)
                new.ZIndex = 2
                new.BackgroundTransparency = 1
                new.FontSize = "Size18"
                new.Font = "SourceSans"
                new.TextXAlignment = "Left"
                new.Text = Weapons[i].Name

This is what i dont want it to do

I dont want it to make 2 labels for the same weapon Name

1 answer

Here I've added a table which is used to contain individual weapon names - it only adds weapons whose names don't yet exist in the table.

local foundWeapons = {}
local Weapons = game.Players.LocalPlayer.Data:WaitForChild("Inventory").Weapons:GetChildren()
             number = 0
             children = #Weapons


            for  i = 1, #Weapons do
	if not table.concat(foundWeapons," ;; "):find(Weapons[i].Name) then
	    local new = Instance.new("TextLabel")
	                new.Parent = script.Parent
	                new.Position = position + UDim2.new(0,0,0,30)
	                position = position + UDim2.new(0,0,0,25)
	                new.Size = UDim2.new(0,200,0,10)
	                new.ZIndex = 2
	                new.BackgroundTransparency = 1
	                new.FontSize = "Size18"
	                new.Font = "SourceSans"
	                new.TextXAlignment = "Left"
	                new.Text = Weapons[i].Name
			table.insert(foundWeapons,Weapons[i].Name)
	end


This discussion is preserved as it was. New answers, comments, and votes are closed.