I created a shop GUI which opens and closes and I created a buy button for my tool but the thing is, it is supposed to give me the tool and take away my current money value (if my money reaches a minimum point, you would be allowed to buy it), it does not work, it does not give me the tool nor take away my money.
local Players = game.GetService("Players") local SS = game:GetService("ServerStorage") local stats = game.Players.player:WaitForChild("leaderstats") script.Parent.MouseButton1Click.Connect(function(buy) if stats.Cash.Value >= 150 then stats.Cash.Value = stats.Cash.Value - 150 local Item = SS.Tools["Pistol"]:Clone() Item.Parent = Players.player.Backpack end end)
What am I supposed to do? What did I do wrong?
EDIT: this script is a server script.
Youre trying to access server storage on what I assume to be a localscript, which is impossible.
To get the player you should use game.Players.LocalPlayer and not game.Players.player
You should use a remote function to handle the purchase on the server, you can never trust the client especially with stuff like this.
In the variable called stats instead of this:
local stats = game.Players.player:WaitForChild(“leaderstats”)
Do this:
local stats = game.Players.LocalPlayer:WaitForChild(“leaderstats”)
See the difference?
So the proper way to do this (if you're using FilteringEnabled which you should be) is to use 2 different scripts 1 local script and 1 server script. You'll want to clone the tool and subtract the money on the server-side and handle the clicking of the button on the local script and connect it to the server script using a remote event.
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
Worth mentioning that GUIs do NOT replicate onto the server and should be dealt with 100% client-side. Also, you should probably not store the money value on the client and should create a separate value on the server likely in ServerStorage.