Hey! So, I used Zednov's tycoon kit to make this game, and he uses a string value for the currency (so the cash could use abbreviations; 1k, 1m, 1b, etc.)
I am trying to make a shop GUI shop and the script I made works totally fine until you take away the money from the user. I'm trying to make it where when you buy something, it gives you the item and takes away your cash. I tried several ways and I thought that I'd bring it here for help. I spent weeks trying to figure out how to fix this and I couldn't find any way.
The output says:
23:12:37.517 - Players.goldenkings11.PlayerGui.Workbench.ScrollingFrame.Bucket.TextButton.LocalScript:7: attempt to perform arithmetic (sub) on string
I truly think the string value has something to do with it and I tried changing it to other values, but the tycoon didn't work properly (whenever you collected cash, your money resetted back to 0). I'm sure this is an easy fix and I'm just blind. I'm not the best at scripting, but I have some experience, so please don't judge me. Thank you in advance!
script.Parent.MouseButton1Click:Connect(function(player) player = game.Players.LocalPlayer if player.leaderstats.Cash.Value >= player.leaderstats.Cash.Value then player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - script.Parent.Price.Value -- the part that doesn't wory print("success") game.Lighting.Tools.Bucket:Clone().Parent = player.Backpack else print("not enough") script.Parent.Text = "Not Enough!" wait (2) script.Parent.Text = "Price: $".. script.Parent.Price.Value end end)
This is a common error I see, your error clearly says that you need to convert your value to a string. We use tostring()
for this problem.
script.Parent.MouseButton1Click:Connect(function(player) player = game.Players.LocalPlayer if player.leaderstats.Cash.Value >= player.leaderstats.Cash.Value then player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - script.Parent.Price.Value -- the part that doesn't wory print("success") game.Lighting.Tools.Bucket:Clone().Parent = player.Backpack else print("not enough") script.Parent.Text = "Not Enough!" wait (2) script.Parent.Text = "Price: $".. tostring(script.Parent.Price.Value) -- tostring end end)
Click view source to copy the code much easier. Accept if it helps.
It seems the issue is due to there being an arithmetic error. You can do arithmetic operations on strings if and only if the numerical value of that string exists.
Example:
local String1 = "56" local String2 = "23" local Result = String1-String2 print(Result) print(typeof(Result)) --//The result ends up being a number
It seems the problem you're having is that either the Price or Cash value isn't numerically represented, say like this:
local String1 = "56K" local String2 = "23" local Result = String1-String2 print(Result)
The result of this code is the same error that you've been experiencing "attempt to perform arithmetic (sub) on string"
Before you subtract the Price from the Cash, put this statement in and tell me the output you get.
print("Cash: " .. player.leaderstats.Cash.Value) print("Price: " .. script.Parent.Price.Value)
(Also just as a side note: on line 3 the comparison is
player.leaderstats.Cash.Value >= player.leaderstats.Cash.Value
which is really weird, shouldn't it be something like
player.leaderstats.Cash.Value >= script.Parent.Price.Value
? Anyway, just a little tangent)