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.
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.