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

What am I doing wrong in this string currency shop script?

Asked by 4 years ago

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)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
I think you completely misread the error message, since it's clearly NOT telling you to convert the value to a string (I already thought it was too?). What the error says is that arithmetic (subtraction) was done on a string. This is only done on line 4. CeramicTile 847 — 4y
0
yes i am dumb rayhansatria 142 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

0
It printed what it was supposed to in output, but how would I make it where it takes the money from the player? I'm sorry for the late response btw, I've been busy all week. I'll be able to respond as soon as you are. Thank you for your help once again! Hopefully we could get through this together. goldenkings11_alt 6 — 4y

Answer this question