Roblox string error

I am currently making a shop on roblox for my simulator, I have come into an issue though can anyone help?

  1. The error is [ServerScriptService.BuyScript:5: attempt to compare Instance and string]

I have tries to do some solutions but they dot work so far.

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local RemoteEvent = ReplicatedStorage:WaitForChild(“BuyTool”)

local function buyTool(player, tool)
if player.leaderstats.Orbs.Value >= tool.Price then
player.leaderstats.Orbs.Value = player.leaderstats.Orbs.Value - tool.Price.Value

end
local giveTool = ReplicatedStorage.ShopItems[tool.Name]:Clone()
giveTool.Parent = player.Backpack

local giveTool = ReplicatedStorage.ShopItems[tool.Name]:Clone()
giveTool.Parent = player.StarterGear

end

RemoteEvent.OnServerEvent:Connect(buyTool)

When i click on the error i go to this line

if player.leaderstats.Orbs.Value >= tool.Price then

What is wrong with this can anyone help please?

Thanks.

Hello!

I’m guessing that there’s is an instance of class “IntValue” or “StringValue” as a child of your tool and the name of this instance is called “Price”.

When doing:

if player.leaderstats.Orbs.Value >= tool.Price then

I believe that player.leaderstats.Orbs.Value refers to the “Value” property of that instance, of which the datatype for that is a string. However, tool.Price would refer to the entire instance of which the datatype is an instance. So when you compare a string to an instance, you get the exception that was printed to your console!

To rectify this, perhaps try if player.leaderstats.Orbs.Value >= tool.Price.Value then in place of that current line of code?

EDIT: Also, I would recommend changing the subclass of your Orbs instance to that of a IntValue or NumberValue if not already done so as suggested by @Xx_FROSTBITExX if you wished to compare the numerical value of the price and orbs. (Which is what I believe you want to do.) Otherwise, if you try to compare two strings, the lua interpreter will simply try to compare the strings alphabetically. (So "a" > "b" will output false, as it is “b” that comes after “a” alphabetically). If you try to compare say a string and a number however, you will get an exception saying “attempt to compare number and string”.

3 Likes

The @Thegamermatt12 is right,

If “Price” is a number value or intvalue, then you have to compare the value itself rather than the name.

If not, then you should consider changing to an IntValue or NumberValue so it’s comparable.

2 Likes

Yes It has worked if i could give you both a solution tick i could. thanks for your help!