Issue with referencing a variable in a function

Hello, I’m trying to create a cosmetics script receiver that changes the color of the player’s cosmetic. However, the script is trying to find an instance called “purchasedvaluename” instead of finding an instance that matches its string value. Is there any way to fix this?

(SCRIPT BELOW:)

function purchaseItem(player, purchasedvaluename, cost, rgb1, rgb2, rgb3)
    local coins = player.leaderstats.Coins
    local cosmetic = player.Character.HumanoidRootPart.Cosmetic
    local purchased = player.CosmeticsValues.purchasedvaluename.Value

    if purchased == true then 
        cosmetic.Color = Color3.fromRGB(rgb1, rgb2, rgb3)
    elseif coins.Value >= cost then --FIX PURCHASE NOT WORKING AS INTENDED
        coins.Value = coins.Value - 5
        cosmetic.Color = Color3.fromRGB(rgb1, rgb2, rgb3)
        purchased = true
        print(purchased)
    else --If there is an error, then print error, print the player's coins, and print their purchased variable
        print("cosmetic error!")
        print(tostring(coins.Value))
        print(purchased)
    end

end

cosmeticevents.GreyBadgePurchase.OnServerEvent:Connect(function(player) 
    purchaseItem(player, "GreyPurchased", false, 5, 70, 70, 70)
end)

cosmeticevents.RedBadgePurchase.OnServerEvent:Connect(function(player) 
    purchaseItem(player, "RedPurchased", false, 5, 185, 0, 0)
end)

cosmeticevents.BlueBadgePurchase.OnServerEvent:Connect(function(player) 
    purchaseItem(player, "BluePurchased", false, 5, 0, 85, 170)
end)

You’re indexing an instance named “purchasedvaluename”, and not what the actual variable is.

You can index instances based on their name in 3 ways:

  1. Using :FindFirstChild so you can validate if the object exists
  2. Using :WaitForChild if you know it’s a valid name but the instance may have not loaded
  3. Using square brackets if you know it’s a valid name and the instance already loaded

I’ll use square brackets here.

Code:

function purchaseItem(player, purchasedvaluename, cost, rgb1, rgb2, rgb3)
    local coins = player.leaderstats.Coins
    local cosmetic = player.Character.HumanoidRootPart.Cosmetic
    local purchased = player.CosmeticsValues[purchasedvaluename].Value

    if purchased == true then 
        cosmetic.Color = Color3.fromRGB(rgb1, rgb2, rgb3)
    elseif coins.Value >= cost then --FIX PURCHASE NOT WORKING AS INTENDED
        coins.Value = coins.Value - 5
        cosmetic.Color = Color3.fromRGB(rgb1, rgb2, rgb3)
        purchased = true
        print(purchased)
    else --If there is an error, then print error, print the player's coins, and print their purchased variable
        print("cosmetic error!")
        print(tostring(coins.Value))
        print(purchased)
    end

end

cosmeticevents.GreyBadgePurchase.OnServerEvent:Connect(function(player) 
    purchaseItem(player, "GreyPurchased", false, 5, 70, 70, 70)
end)

cosmeticevents.RedBadgePurchase.OnServerEvent:Connect(function(player) 
    purchaseItem(player, "RedPurchased", false, 5, 185, 0, 0)
end)

cosmeticevents.BlueBadgePurchase.OnServerEvent:Connect(function(player) 
    purchaseItem(player, "BluePurchased", false, 5, 0, 85, 170)
end)

You could also do all of this under one remote event, will be easier to manage especially when you add more items to purchase.

Ty! Sry btw but for some reason its saying cost is a boolean?

Yeah, you’re sending over a boolean right here:

Not sure what’s the problem, unless you didn’t mean to type that in?

You can also send over RGB values instead of each red, green and blue value individually.

O wait nvm sry I added an extra parameter

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