Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

For some reason the math won't work on my buying script?

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)

2 answers

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.

2 comments

0

Thank You very much!

0

my script won't get past line 24

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

1 comment

0

No. This will DEFINITELY not work. PlayerGuis should be handled LOCALLY. "leaderstats" is also not a child of game. It would error. I suggest you edit your answer because of the many flaws you have made. You are also defining Player as game? Please look at my answer if you're confused!

This discussion is preserved as it was. New answers, comments, and votes are closed.