General Practice
Use :WaitForChild() to determine if a part exists before using / changing it
(you did do this, but to check warnings, it's best to continue this throughout the script)
Use :GetService() to retrieve the Players and ReplicatedStorage Services
Issues
Do not use :FindFirstChild() on a Table, either iterate through it or call the index value directly
You cannot perform math on a string, so you need to change it to a number using tonumber() before doing arithmetic
Revised Local Script V1
local player = game:GetService("Players").LocalPlayer
local ownedSkins = player:WaitForChild("PlayerSkins")
local chosenKnife = player:WaitForChild("SelectedKnifeSkin")
local chosenGun = player:WaitForChild("SelectedGunSkin")
local skins = game:GetService("ReplicatedStorage"):WaitForChild("Skins")
local template = game:GetService("ReplicatedStorage"):WaitForChild("SkinTemplate")
local scroll = script.Parent:WaitForChild("Frame"):WaitForChild("ScrollingFrame")
local invSkins = nil
ownedSkins.ChildAdded:Connect(function(child)
if scroll:FindFirstChild(child.Name) == nil then
local rarity = child:WaitForChild("Rarity")
local nTemplate = template:Clone()
local label = nTemplate:WaitForChild("TextLabel")
nTemplate.Parent = player:WaitForChild("PlayerGui"):WaitForChild("Menu"):WaitForChild("Inventory"):WaitForChild("Frame"):WaitForChild("ScrollingFrame")
nTemplate.Image = ("rbxassetid://"..(child:WaitForChild("Icon").Value))
nTemplate.Name = child.Name
label.Text = child.Name
if rarity.Value == "Common" then
label.TextColor3 = Color3.fromRGB(140, 139, 144)
elseif rarity.Value == "Uncommon" then
label.TextColor3 = Color3.fromRGB(75, 88, 212)
elseif rarity.Value == "Legendary" then
label.TextColor3 = Color3.fromRGB(255, 198, 83)
end
elseif scroll:FindFirstChild(child.Name) ~= nil then
local skinInv = scroll:FindFirstChild(child.Name)
local label = skinInv:WaitForChild("Amount"):WaitForChild("TextLabel")
label.Text = tonumber(label.Text) + 1
end
invSkins = scroll:GetChildren()
end)
Explanation #1
The main issue with your code is that multiple frames are being created, which can only occur if the "child.Name" is not the same as any of the children of the ScrollingFrame. However, since this code doesn't show how the values are added to the player.PlayerSkins folder, then I can't extrapolate where the difference occurs just reading your code. However, I will instead give you a revision below that will help you determine what the change is.
Example Code
Below, the output should print out the child's name and the names of the children of the Scrolling Frame, as well as a statement denoting if the name of the child and the scroll item match (it will print nil if they don't), which is returned by string.match()
print("The child's name is "..child.Name)
for a, b in pairs(scroll:GetChildren()) do
print("The scroll item's name is "..b.Name)
print("Match Result: "..string.match(b.Name, child.Name, 1))
end
Revised Local Script V2
local player = game:GetService("Players").LocalPlayer
local ownedSkins = player:WaitForChild("PlayerSkins")
local chosenKnife = player:WaitForChild("SelectedKnifeSkin")
local chosenGun = player:WaitForChild("SelectedGunSkin")
local skins = game:GetService("ReplicatedStorage"):WaitForChild("Skins")
local template = game:GetService("ReplicatedStorage"):WaitForChild("SkinTemplate")
local scroll = script.Parent:WaitForChild("Frame"):WaitForChild("ScrollingFrame")
local invSkins = nil
ownedSkins.ChildAdded:Connect(function(child)
print("The child's name is "..child.Name)
for a, b in pairs(scroll:GetChildren()) do
print("The scroll item's name is "..b.Name)
print("Match Result: "..string.match(b.Name, child.Name, 1))
end
if scroll:FindFirstChild(child.Name) == nil then
local rarity = child:WaitForChild("Rarity")
local nTemplate = template:Clone()
local label = nTemplate:WaitForChild("TextLabel")
nTemplate.Parent = player:WaitForChild("PlayerGui"):WaitForChild("Menu"):WaitForChild("Inventory"):WaitForChild("Frame"):WaitForChild("ScrollingFrame")
nTemplate.Image = ("rbxassetid://"..(child:WaitForChild("Icon").Value))
nTemplate.Name = child.Name
label.Text = child.Name
if rarity.Value == "Common" then
label.TextColor3 = Color3.fromRGB(140, 139, 144)
elseif rarity.Value == "Uncommon" then
label.TextColor3 = Color3.fromRGB(75, 88, 212)
elseif rarity.Value == "Legendary" then
label.TextColor3 = Color3.fromRGB(255, 198, 83)
end
elseif scroll:FindFirstChild(child.Name) ~= nil then
local skinInv = scroll:FindFirstChild(child.Name)
local label = skinInv:WaitForChild("Amount"):WaitForChild("TextLabel")
label.Text = tonumber(label.Text) + 1
end
invSkins = scroll:GetChildren()
end)
2 comments
Can you please add an image of your "Explorer" tab? Since the issue seems to be that the "child" being added to the "PlayerSkins" does not share its name with a child of the ScrollingFrame
Yes, here. https://imgur.com/a/1HvzJd3