I need help with my script

hi i have a problem. When I want to take money from a player, I get this error:
attempt to perform arithmetic (sub) on string and number

my script:

 local player = game.Players.LocalPlayer
 local stats = player:WaitForChild("leaderstats")
 local Cash = stats:WaitForChild("Cash")

 script.Parent.MouseButton1Click:Connect(function()
 Cash.value = Cash.value - 500000

how can i fix this error?

2 Likes

It’s Cash.Value, not Cash.value.

1 Like

Is Cash an IntegerValue or NumberValue?

And make sure to capitalize .Value

1 Like

Btw changing Stats on Client make change only for the client.

2 Likes

Did you make your Cash a StringValue? And as @caviarbro mentioned, you need to use a RemoteEvent to be able to properly change the value as client sided changes wont replicate to the server

2 Likes

Not 100% sure but I think it should be

local Cash = leaderstat:WaitForChild("Cash")

But then again I am not 100% sure

1 Like

No, what they put is correct as they defined the leaderstats variable as stats not leaderstat.

2 Likes

Unfortunately, none of the solutions worked for me. I also used as a basis Zed’s tycoon kit and I spent a lot of time but didnt find the line in which Cash was created…

Print out every variable that you defined outside of the MouseButton1Click Event? If everything looks good, chances are you’re probably using a StringValue instead? Should look something like this:

local player = game.Players.LocalPlayer
local stats = player:WaitForChild("leaderstats")
local Cash = stats:WaitForChild("Cash")

print(player)
print(stats)
print(Cash)

script.Parent.MouseButton1Click:Connect(function()
    Cash.Value = Cash.Value - 500000
end)
2 Likes

Yes! it works! Thank you very mach for your help!!!

1 Like

Anytime! Also as others mentioned, I would recommend looking up RemoteEvents so that you’re capable of checking the Cash value anywhere if you want to perform any sanity checks :wink:They’re pretty cool

1 Like

yes, i will try to study it :grinning: