I have an intvalue as Cash
--Variables-- local Item = game.Workspace.Guns.FourthGun:Clone() local Button = script.Parent local Player = game.Players.LocalPlayer local Cost = 50 local Cash = Player.leaderstats.Cash.Value local Mouse = Player:GetMouse() local PurchaseButton = script.Parent.BuyPrompt.PurchaseButton local LeaveButton = script.Parent.BuyPrompt.LeaveButton --Actual scripting-- 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() Cash = Cash - Cost end)
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.
--Variables-- 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 --Actual scripting-- 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.
--A server script in ServerScriptService local RemoteEvent = game:GetService("ReplicatedStorage").RemoteEvent local Item = game:GetService("ServerStorage").Guns.FourthGun --ALWAYS handle clone tools on server side. 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.
Basically There shouldn't be anything wrong but the script could be confused and I will show how I changed it up. MAKE THIS A SERVER SIDE SCRIPT NOT LOCAL
--Variables-- local Item = game.Workspace.Guns.FourthGun:Clone() local Button = script.Parent local lead = game:WaitForChild("leaderstats") local Player = lead.Parent local Cost = 50 local Cash = Player.leaderstats.Cash local Mouse = Player:GetMouse() local PurchaseButton = script.Parent.BuyPrompt.PurchaseButton local LeaveButton = script.Parent.BuyPrompt.LeaveButton --Actual scripting-- 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() Cash.Value -= Cost--Heres you can do it easier using -= so you dont gotta have cash twice end)
if this works or helps accept