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

How would I make button deduct certain amount of currency?

Asked by
DrShibe 17
7 years ago

So I've tried multiple ways on making it so the button in the SGUI takes money away but cant seem to find a way. It basically is a SGUI where you can purchase a gun and I can't seem to make a way. The code is as is.

Buy = script.Parent
Gun = game.Lighting.Pistol

Buy.MouseButton1Down:connect(function(purchase)
game.Lighting["Pistol"]:Clone().Parent = game.Players.LocalPlayer.Backpack
print "gave gun"


end)

In workspace, I have a freemodel money leaderboard script and the currency is "Money"

1 answer

Log in to vote
2
Answered by 7 years ago

Hi there! I'm BlackOrange3343 and it's time to help!

Here's your script;

Buy = script.Parent
Gun = game.Lighting.Pistol

Buy.MouseButton1Down:connect(function(purchase)
game.Lighting["Pistol"]:Clone().Parent = game.Players.LocalPlayer.Backpack
print "gave gun"


end)

And you want to add a function so that it will take money away. First, we want to make a few changes before advancing.

A few changes;

local Buy = script.Parent -- Added local in front
local Gun = game.Lighting:WaitForChild("Pistol") -- Added local in front and WaitForChild function, WaitForChild waits for the gun to load in or else it may not work in the real game.

Buy.MouseButton1Down:connect(function(purchase)
game.Lighting["Pistol"]:Clone().Parent = game.Players.LocalPlayer.Backpack
print "gave gun"


end)

The thing you should look out for is the Gun variable. As I take a look the gun variable isn't being used in the script, anyway, let's move on.

Now you want to make sure you set a price of how much you want to sell the gun for as a variable.

local Cost = 100

Let's use this as an example.

Now you want to make it so that once clicked if the player has enough money it will work.

Add an "if" statement;

if player.leaderstats.Cash.Value >= Cost then

Because I'm writing this right here, I'm not sure if ">=" is correct so if it isn't correct then change it to "=>". Also, you should make sure the name is correct if it's Cash or Money etc. Change it to the correct currency. And lastly, you should look out if your leaderstats is stored in leaderstats. It may be stored somewhere else.

Now the final script should look like this;

local Buy = script.Parent -- Added local in front
local Gun = game.Lighting:WaitForChild("Pistol") -- Added local in front and WaitForChild function, WaitForChild waits for the gun to load in or else it may not work in the real game.
local Cost = 100

Buy.MouseButton1Down:connect(function(purchase)
    if player.leaderstats.Cash.Calue >= Cost then
    game.Lighting["Pistol"]:Clone().Parent = game.Players.LocalPlayer.Backpack
    wait()
    print "gave gun"
    end
end)

I suggest you add an else statement is not working.

It would look like this;

local Buy = script.Parent -- Added local in front
local Gun = game.Lighting:WaitForChild("Pistol") -- Added local in front and WaitForChild function, WaitForChild waits for the gun to load in or else it may not work in the real game.
local Cost = 100

Buy.MouseButton1Down:connect(function(purchase)
    if player.leaderstats.Cash.Calue >= Cost then
    game.Lighting["Pistol"]:Clone().Parent = game.Players.LocalPlayer.Backpack
    wait()
    print ("gave gun")
else -- else 
    print("Not enough cash")
    end
end)

Thank you for reading, I hope this helped. Make sure to Upvote and accept the answer if helped. Otherwise, comment on any question or errors and I will make sure to help you once again.

0
Script:6: attempt to index global 'player' (a nil value) DrShibe 17 — 7y
0
oops BlackOrange3343 2676 — 7y
1
Ok DrShibe, make a new variable, call it player and say local player = game.Players.LocalPlayer that should work. If worked then upvote and accept BlackOrange3343 2676 — 7y
0
As a local script . nothing happens, as a normal script I added that variable and then it still says it failed to index upvote 'Player" (a nil value) on same line DrShibe 17 — 7y
View all comments (13 more)
0
but the local script inside a button BlackOrange3343 2676 — 7y
0
Also your money currency is Money so change Cash with Money, make sure your model is called leaderstats in player as well BlackOrange3343 2676 — 7y
0
Check output also for the print saying not enough money BlackOrange3343 2676 — 7y
0
Nothing happens. Nothing in the output either. DrShibe 17 — 7y
0
Also the <= or =< might be wrong BlackOrange3343 2676 — 7y
0
I noticed a issue. It says Calue instead of value. DrShibe 17 — 7y
0
Use the print method to check, use print as checkpoints to see where went wrong, so put print something after clicked and put print after the if statment print can be anything it's just checkpoints BlackOrange3343 2676 — 7y
0
DUDe then i t's obvious that it's Value BlackOrange3343 2676 — 7y
0
Sorry about that it's Value now it should work BlackOrange3343 2676 — 7y
0
it still doesn't work. Want me to teamcreate with you? DrShibe 17 — 7y
0
I added you, DrShibe was my old account that got term'd. I'm LordExotec and I'll share the place with you DrShibe 17 — 7y
0
Lol I will check on it BlackOrange3343 2676 — 7y
0
Okay I added you, accept + ill add you into the team create. DrShibe 17 — 7y
Ad

Answer this question