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"
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.