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

Perform Arithmetic with IntValue.Value wont work?

Asked by
xoxLua 0
4 years ago
Edited 4 years ago

This is the part that i cant solve the problem:

remotes.BuyItem.OnServerEvent:Connect(function(player, itemName)
    local priceItem = game.ServerStorage.Items[itemName]
    local price = priceItem.Price.Value
    local coins = player.PlayerData:WaitForChild("Coins")

    -- Under here is the problem
    if coins >= price then
        coins = coins - price

The error i get is: ****bad argument #2 to '?' (string expected, got Object)

Could someone tell me what failed and how to fix it?

1 answer

Log in to vote
0
Answered by 4 years ago

You are comparing a number value with a object value, do this

remotes.BuyItem.OnServerEvent:Connect(function(player, itemName)
    local priceItem = game.ServerStorage.Items[itemName]
    local price = priceItem.Price.Value
    local coins = player.PlayerData:WaitForChild("Coins")

    -- Under here is the problem
    if coins.Value >= price then
        coins.Value = coins - price

or you can do

remotes.BuyItem.OnServerEvent:Connect(function(player, itemName)
    local priceItem = game.ServerStorage.Items[itemName]
    local price = priceItem.Price.Value
    local coins = player.PlayerData:WaitForChild("Coins")
coins = coins.Value

    -- Under here is the problem
    if coins >= price then
        coins = coins - price

also, dont forget to end BuyItem.OnServerEvent and if statement! if this helped mark this as correct :D

0
intvalue* maumaumaumaumaumua 628 — 4y
0
now it says "attempt to compare string with number". xoxLua 0 — 4y
0
whichever value is the string that youre having problems with, as long as that string is displaying a number and not actual letters, use tonumber() itll convert it into a number value Si_SenorTN 25 — 4y
0
also, a few things. you cannot call .Value when you are defining the variable, it must be called within your if statements or any other instance of you calling an value object within your code. and lastly you do not need a remote event to tell the server your local player is about to buy something. do not have a server script handle a local players stats, this can all be done from a local script. Si_SenorTN 25 — 4y
Ad

Answer this question