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.
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)
Why does it need to be a StringValue though? 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
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,
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
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 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)
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)
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.