Broken Purchase Script

The goal that I want to achieve with this is to have a car spawner that will take your money away when you purchase a car.

The issue is that when I purchase a car, the money used to purchase said car does not decrease on the leaderstats. Lets say I have $100,000 and the car I want is $70,000. If I were to purchase that car, my initial money doesn’t decrease at all.

I have tried to rework the leaderstat positions and names, as well as the setup of how it’s scripted. Nothing I’m trying is working.

The part of the script I posted is only the function of purchasing the car. There are more lines above this

Within the script itself, there is a module script with each car for sale. That’s what the "veh[number]"s connect to within the script.

I’m honestly clueless lol

what’s plrMoney?
Passing a value will not change the value itself

So if I were to replace “plrMoney” with something like
“ourPlayer.leaderstats.Money.Value = ourPlayer.leaderstats.Money.Value - price”
I would think that would help my problem. But the problem continues. This is the first time I’ve ever gotten into datastore scripting and stuff, so I’m honestly clueless with all of this

just do Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - price
replacing plr money = plrmoney - price

And maybe, “veh[6]” Might be nil, print it to see

Alright, I’ll try it out. Thanks.

Also, I should have told you that “ourPlayer” is a local variable at the top of the script

Is this run in a LocalScript? Because then the changes won’t replicate to the server. If it’s ran in a Script, then you won’t be able to access LocalPlayer because it’s nil on the server.

This is ran in a normal script.

So this line:

local ourPlayer = game.Players.LocalPlayer

It’s stored in a Script and not a LocalScript? Because then there is no point in storing it since the value is always nil server-sided.

Do you have any input as to what I should change to make that line cooperate with the rest of the script?

It’s no different than what @varjoy has suggested.

His suggestion gave me the red-line. I’m honestly not sure what is wrong with this script.

Would I need to completely remove this line? Is there anything I could replace it with?

local ourPlayer = game.Players.LocalPlayer

Like I was saying before, this is my first time working with types of scripts like this. I’m not the most educated in this area.
Sorry, I’m absolutely clueless

Don’t worry, scripting is kinda hard to learn. FireServer() automatically passes over the Player instance that uses it, which you have already acknowledged and specified as “plr”. Simply replace “Player” with “plr” and you’re good to go.

Alright, so I just ran a couple tests. I added a few more areas in the script where it’ll print a message. Where it says print("Pre-Buy"), that is the last line that is run before there’s an error. The error occurs with the if-statement on leaderstats

Once it hits the print-statement, the scripts functionality ends. The money isn’t taken

I’m assuming you either didn’t specify veh[6] or specified it as something that’s not a number.

So with this system, theres a few different scripts. There’s a script in serverscriptservice, and that’s the one that I’ve been working on with the whole plr vs player and localplayer topic. Theres a module script within a GUI in a clickpart, and thats where the veh[number]'s are.

I commented which veh’s are which in the script.

I’m not sure what I’d do to properly define veh[6] in the serverscriptservice script.

Try printing veh[6]. That’ll show us if the problem is with veh[6] or not.

Alright so, I tried purchasing two cars. One car was worth $0, and the other was worth $20,000. The free car purchased perfectly fine, and saved to my player. The $20,000 dollar car did not purchase, and did not save to my player.

Here’s a screenshot of the output:
Screen Shot 2020-05-31 at 10.36.09 AM

At the bottom, where it says “20000”, that’s the print of print(price) (price = veh[6])

It might be because plr.leaderstats.Money.Value is not the same int/double value as price. Try doing this:

if tonumber(plr.leaderstats.Money.Value) >= tonumber(price) then

That was it! It’s fixed! Thank you so much for your help.

1 Like

No problem. Make sure you mark my solution as… well… the Solution so that other people know it’s already solved.

1 Like