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
local Gun = game.Lighting:WaitForChild("Pistol")
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
local Gun = game.Lighting:WaitForChild("Pistol")
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
local Gun = game.Lighting:WaitForChild("Pistol")
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
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
Script:6: attempt to index global 'player' (a nil value)
oops
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
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
but the local script inside a button
Also your money currency is Money so change Cash with Money, make sure your model is called leaderstats in player as well
Check output also for the print saying not enough money
Nothing happens. Nothing in the output either.
Also the <= or =< might be wrong
I noticed a issue. It says Calue instead of value.
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
DUDe then i t's obvious that it's Value
Sorry about that it's Value now it should work
it still doesn't work. Want me to teamcreate with you?
I added you, DrShibe was my old account that got term'd. I'm LordExotec and I'll share the place with you
Lol I will check on it
Okay I added you, accept + ill add you into the team create.