Hi, I'm making a skin system for my game and i'm trying to get it so the amount updates for how many skins you have, but it just adds a new one everytime! (I.E It should say 5 soda skins but it makes 5 soda skin frames) Please help!
local player = game.Players.LocalPlayer local ownedSkins = player:WaitForChild("PlayerSkins") local skins = game.ReplicatedStorage.Skins local template = game.ReplicatedStorage.SkinTemplate local invSkins = script.Parent.Frame.ScrollingFrame:GetChildren() local chosenKnife = player:WaitForChild("SelectedKnifeSkin") local chosenGun = player:WaitForChild("SelectedGunSkin") ownedSkins.ChildAdded:Connect(function(child) if script.Parent.Frame.ScrollingFrame:FindFirstChild(child.Name) == nil then local nTemplate = template:Clone() nTemplate.Parent = player.PlayerGui.Menu.Inventory.Frame.ScrollingFrame nTemplate.Image = "rbxassetid://"..child:WaitForChild("Icon").Value nTemplate.TextLabel.Text = child.Name nTemplate.Name = child.Name if child.Rarity.Value == "Common" then nTemplate.TextLabel.TextColor3 = Color3.fromRGB(140, 139, 144) elseif child.Rarity.Value == "Uncommon" then nTemplate.TextLabel.TextColor3 = Color3.fromRGB(75, 88, 212) elseif child.Rarity.Value == "Legendary" then nTemplate.TextLabel.TextColor3 = Color3.fromRGB(255, 198, 83) end elseif script.Parent.Frame.ScrollingFrame:FindFirstChild(child.Name) ~= nil then local skinInv = invSkins:FindFirstChild(child.Name) skinInv.Amount.TextLabel.Text = skinInv.Amount.TextLabel.Text+1 end invSkins = script.Parent.Frame.ScrollingFrame:GetChildren() end)
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
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
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)
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.
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
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)