(Solved) How to change a player's money value with a certain number variable after clicking a GUI using RemoteEvents

I’m trying to make it so when a player clicks a buy button, 60 “tix” is taken away from them. The number of tix to take away is under a variable named “price”, but I don’t know how to listen for the variable on the server script.

Local (In buy button):

local player = game.Players.LocalPlayer
local tix = player.leaderstats.Tix.Value

local button = script.Parent

local price = 60

local event = game.ReplicatedStorage.ChargePlayerTix

button.MouseButton1Click:Connect(function()
	if tix >= price then
		event:FireServer(price)
	end
end)

Server (in StarterPlayerScripts):

local tix = script.Parent.Parent.leaderstats.Tix
local event = game.ReplicatedStorage.ChargePlayerTix

event.OnServerEvent:Connect(function(price)
	tix.Value = tix.Value - price
end)

The reason your script is not working is because the first argument of an OnServerEvent listener is equal to the player that sent the event. However, I discourage you from doing this like this, because it is very vulnerable to exploitation. Someone can simply fire the event with a negative number and make money.

The simple solution

If you’d like to ignore the vulnerabilities with this, and just want to fix your issue, simply make these changes to your server script:

local tix = script.Parent.Parent.leaderstats.Tix
local event = game.ReplicatedStorage.ChargePlayerTix

event.OnServerEvent:Connect(function(player, price)
	tix.Value -= math.abs(price)
end)

The better solution

If you’d also like to fix these vulnerabilities that I mentioned, you should store the prices in a table on the server, and double check that everything matches up. Inquire for more information on this.

I applied the changes, and nothing happens, but there is no error in the output or script analysis.

Where is this script located? If it is in the player, then it will not work.

It’s in StarterPlayerScripts, I’m not really sure if that’s the same thing or not

I would suggest moving this script to ServerScriptService and making these changes:

local event = game.ReplicatedStorage:WaitForChild("ChargePlayerTix")

event.OnServerEvent:Connect(function(player, price)
    local tix = player.leaderstats.Tix
	tix.Value -= math.abs(price)
end)
local player = game.Players.LocalPlayer
local tix = player.leaderstats.Tix

local button = script.Parent

local price = 60

local event = game.ReplicatedStorage.ChargePlayerTix

button.MouseButton1Click:Connect(function()
	if tix.Value >= price then
		event:FireServer(price)
	end
end)

--------------------------------------------------------------------

local tix = script.Parent.Parent.leaderstats.Tix
local event = game.ReplicatedStorage.ChargePlayerTix

event.OnServerEvent:Connect(function(price)
	tix.Value -= price
end)
-- move to serverscriptservice, and change paths as needed.
1 Like

Thanks, I will definitely look into the better solution if exploiting becomes an issue, which it probably will