Having trouble with Tycoon

Hello, everyone! Im having some scripting issues. Im making a FPS/Tycoon. When you buy a gun(walk over the desired button), a new station appears in front of you. The station has a button, and when you click the button, you spent money and you get a tool. However, when I click the button, I get an issue where output says:

  13:44:02.724  'Workspace.Buy AKM Table.BuyAKM.Script:7: attempt to compare number and string' -  Server - Script:7
  13:44:02.725  Stack Begin  -  Studio
  13:44:02.725  Script 'Workspace.BuyAKMTable.BuyAKM.Script', Line 7  -  Studio - Script:7
  13:44:02.725  Stack End  -  Studio

Does anyone have any ideas?
If your wondering, the tycoon’s cash value is a string value, but if I change it to a int/num value, it breaks.

can you show the actual script that is erroring?

Use tonumber().

e.g tonumber(cash.Value)

Sure, here it is.

local cost = 1000
local db = true

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	if player.leaderstats.Cash.Value >= cost then
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - cost
	db = false
	game.ReplicatedStorage.BuyGun["AKM"]:clone().Parent = player.Backpack
	db = true
	end
end)

Where should I be using tonumber(Cash.value) in my script?

Why does it need to be a StringValue though? :thinking: Anyways like @beanandsoupguy said just use the tonumber() function to convert a string to a number like so:

local cost = 1000
local db = true

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local Cash = tonumber(player.leaderstats.Cash.Value)

	if Cash >= cost then
	    Cash -= cost
	    db = false
	    game.ReplicatedStorage.BuyGun["AKM"]:clone().Parent = player.Backpack
	    db = true
	end
end)

It needs to be a StringValue because the main script changes the player’s cash value from being 100,000 to 100K.

Anyways, I wish this could’ve been the solution. Now, the output says:

  14:36:49.565  'Workspace.Buy AKM Table.BuyAKM.Script:7: attempt to compare number and nil'  -  Server - Script:7
  14:36:49.565  Stack Begin  -  Studio
  14:36:49.565  Script 'Workspace.Buy AKM Table.BuyAKM.Script', Line 7  -  Studio - Script:7
  14:36:49.565  Stack End  -  Studio

H u h
Ok maybe I messed something up, try this?

local cost = 1000
local db = true

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    tonumber(player.leaderstats.Cash.Value)
    print(player.leaderstats.Cash.Value)

	if player.leaderstats.Cash.Value >= cost then
	    player.leaderstats.Cash.Value -= cost
	    db = false
	    game.ReplicatedStorage.BuyGun["AKM"]:clone().Parent = player.Backpack
	    db = true
	end
end)

Sadly, no. The output is back to the beginning. Output:

  14:49:47.402  Workspace.Buy AKM Table.BuyAKM.Script:8: attempt to compare number and string  -  Server - Script:8
  14:49:47.402  Stack Begin  -  Studio
  14:49:47.402  Script 'Workspace.Buy AKM Table.BuyAKM.Script', Line 8  -  Studio - Script:8
  14:49:47.402  Stack End  -  Studio

Im still pretty new to scripting, and I read line,

script.Parent.ClickDetector.MouseClick:Connect(function(player)

Do you think the clickdetector is inside of the part, “BuyAKM?” Or, do you think the script is inside of the clickdetector? Maybe this helps, I have no clue. All I can really say right now is your very helpful :slight_smile:

AKMTable

Nah, the way you’re connecting your event is fine as it’s parent order is being assigned as this:

script > BuyAKM > ClickDetector > Event

So it can’t be that

Bit strange though that it’s still attempting to compare it as a string :thinking: Here’s what my guess is: Since you mentioned earlier that

It’s defining as nil, because you need to find some way to abbreviate the Cash StringValue into turning as an actual number value

I know this won’t work, but if you print the Value of the Cash stat, it should be something like 4K when it’s outputted:

local cost = 1000
local db = true

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    print(player.leaderstats.Cash.Value)
    local Cash = tonumber(player.leaderstats.Cash.Value)

	if Cash >= cost then
	    Cash  -= cost
	    db = false
	    game.ReplicatedStorage.BuyGun["AKM"]:clone().Parent = player.Backpack
	    db = true
	end
end)

Yes, when I checked the output, the script printed “15K+”

Wait, are you saying if I find out a way to abbreviate the Cash StringValue from “1000,” to “1K,” the script could work?

Pretty much, yes

Cause the error you’re getting is that you’re still attempting to compare a string (5K) with a number (Cost requirement value)

1 Like

That won’t work, it’s abbreviated as a StringValue being as (5K, 10K, 25K, etc)

Oh right, hm. I am unsure what the solution would be then.

I see the problem. You need to have a number value showing the actual amount of cash because if you put, for example, “4K” through tonumber, it will come as nil. So you need to have a numbervalue hidden for that, which would be 4K. Reply if you don’t get it, I can probably explain it better if needed.

What you need to do is you need to parse the StringValue to a number. This cannot be directly done using tonumber, cause tonumber is only made for numerical strings (strings that only contain numbers and not characters).

local cost = 1000
local db = true

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local cashValue = player.leaderstats.Cash.Value
    -- Notice that we remove the 'K' at the end
    local cash = tonumber(string.sub(cashValue, 0, string.len(cashValue) - 1)) * 1000
	if cash >= cost then
        -- Now add the K back again
	    cashValue.Value = tostring((cash - cost) / 1000) .. 'K'
	    db = false
    	game.ReplicatedStorage.BuyGun["AKM"]:clone().Parent = player.Backpack
	    db = true
	end
end)
2 Likes

The output now states:

  15:30:32.688  'Workspace.Buy AKM Table.BuyAKM.Script:7: attempt to perform arithmetic (mul) on string and number'  -  Server - Script:7

What does this mean, especially “(mul)?”

I forgot a ‘)’, could you try again?

I did that earlier, since the “if” statement had a red line. The “)” was added after the “1000,” unless you want me to add it to another area.

edit: I re-copy and pasted the current script you re-edited. Same output.