(Roblox) " attempt to index number with 'Value' " and " Value is not a valid member of LocalScript "

Viewed 52

I'm new in Roblox I don't know what I'm doing wrong,

Why do I get this error? attempt to index number with 'Value Script:

local milk = game.ReplicatedStorage.Milk
local price = 10

script.Parent.MouseButton1Click:Connect(function()
    
    if milk >= price.Value then
        milk = milk - price.Value
        price = price.Value + 10
    else
        print("error")
    end
end)

Why do I get this error? Value is not a valid member of LocalScript Script:

local price = script.Parent.Parent.BuyButton:FindFirstChild("shopSystem")

while true do
    script.Parent.Parent.PriceText.Text = price.Value .. " Milk"
end

Thanks

2 Answers

You get this error because you are attempting to index a number with Value.

 if milk >= price.Value then
        milk = milk - price.Value
        price = price.Value + 10

price is a number value You cannot index numbers.

Use

if milk >= price then
        milk = milk - price
        price = price + 10

Assuming milk is some Roblox type like IntValue and not a native Lua number you should use milk.Value for any calculations. if milk.Value >= price then milk.Value = milk.Value - price price = price + 10

Second error

local price = script.Parent.Parent.BuyButton:FindFirstChild("shopSystem")

Here you assign a LocalScript to price. Then you attempt to index that LocalScript with Value. LocalScript does not have a Value field.

Don't do price.Value to avoid that error. Also the naming is not very meaningful. Why would you name a LocalScript price?

And don't run your code in an infinite while loop

Is milk a int? If so try this:

local milk = game.ReplicatedStorage.Milk.Value
local price = 10

script.Parent.MouseButton1Click:Connect(function()
    
    if milk >= price.Value then
        milk = milk - price.Value
        price = price.Value + 10
    else
        print("error")
    end
end)
Related