Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When joining or buying skin amount doesn't update?

Asked by 5 years ago

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)
0
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 SerpentineKing 3885 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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)
0
I tried the revised local script v2 and it kept creating a (i don't know what to call it so i'll call it a frame) frame everytime. ayeayeCommsta 24 — 5y
0
what did the output say though? that's the reason for v2 SerpentineKing 3885 — 5y
0
13:37:02.646 - Players.ayeayeCommsta.PlayerGui.Menu.Inventory.UpdateInventory:14: attempt to concatenate a nil value 13:37:02.646 - Stack Begin 13:37:02.648 - Script 'Players.ayeayeCommsta.PlayerGui.Menu.Inventory.UpdateInventory', Line 14 13:37:02.648 - Stack End ayeayeCommsta 24 — 5y
0
try the same thing except remove line 14 with the match result, it shouldnt be required anyway SerpentineKing 3885 — 5y
0
didn't work. still made a frame everytime ayeayeCommsta 24 — 5y
Ad

Answer this question