Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make button deduct certain amount of currency?

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

✓ Accepted answer

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.

17 comments

0

Script:6: attempt to index global 'player' (a nil value)

0

oops

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

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

0

but the local script inside a button

0

Also your money currency is Money so change Cash with Money, make sure your model is called leaderstats in player as well

0

Check output also for the print saying not enough money

0

Nothing happens. Nothing in the output either.

0

Also the <= or =< might be wrong

0

I noticed a issue. It says Calue instead of value.

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

0

DUDe then i t's obvious that it's Value

0

Sorry about that it's Value now it should work

0

it still doesn't work. Want me to teamcreate with you?

0

I added you, DrShibe was my old account that got term'd. I'm LordExotec and I'll share the place with you

0

Lol I will check on it

0

Okay I added you, accept + ill add you into the team create.

This discussion is preserved as it was. New answers, comments, and votes are closed.