Never, NEVER, NEVER ever handle player leaderstats and purchases on the client. It makes the job easy for exploiters to give themselves cash. Instead, handle it server side.
local Button = script.Parent
local Player = game:GetService("Players").LocalPlayer
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local PurchaseButton = script.Parent.BuyPrompt.PurchaseButton
local LeaveButton = script.Parent.BuyPrompt.LeaveButton
script.Parent.MouseButton1Down:Connect(function()
script.Parent.BuyPrompt.Visible = true
end)
LeaveButton.MouseButton1Down:Connect(function()
script.Parent.BuyPrompt.Visible = false
end)
PurchaseButton.MouseButton1Down:Connect(function()
RemoteEvent:FireServer("FourthGun")
end)
We will be using RemoteEvent to communicate client to server.
local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent
local Item = game:GetService("ServerStorage").Guns.FourthGun
RemoteEvent.OnServerEvent:Connect(function(Player, item)
if item == Item.Name then
local Cash = Player.leaderstats.Cash
if Cash.Value >= 50 then
Cash.Value = Cash.Value - 50
Item = Item:Clone()
Item.Parent = Player.Backpack
end
end
end)
Read more about RemoteEvents here: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
I'll explain here what the heck is going on in the script.
A client is a player, the server is the game that a client does not replicate to. Many people make the mistake of handling "purchases" client side. You should ALWAYS asume the client is an exploiter. Exploiters can always spoof scripts to give themselves advantages. And you cannot change a player's cash through the client and expect it to save. Datastores are managed server-side, and cannot see what the client is seeing. That means you CANNOT save player's leaderstats client side.
2 comments
Thank You very much!
my script won't get past line 24