Help with Remote Event Purchase Script

I am having a problem with the serverscript that I made. How can I fire the price value from my client to the server? It works if I put “player.leaderstats.Points.Value = player.leaderstats.Points.Value -10” but my items have different prices. Thank you.

SERVER SCRIPT
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyItem = game.ReplicatedStorage.BuyItem

local function BuyThisItem(player,price)
	player.leaderstats.Points.Value = player.leaderstats.Points.Value -price <--MY PROBLEM
end

BuyItem.OnServerEvent:Connect(BuyThisItem)


CLIENT
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyItem = ReplicatedStorage:WaitForChild("BuyItem")

local price = 10
script.Parent.MouseButton1Click:Connect(function(m)
	if player.leaderstats.Points.Value >= price then
		BuyItem:FireServer(player,price)
	end
end)

It gives me an error that says “attempt to perform arithmetic (sub) on number and Instance”. Please help, thank you.

1 Like

When firing an event to the server there is no need to send over the player as the first argument. OnServerEvent already has the player as the first parameter automatically. You are basically using the player as the price.

Instead do:
BuyItem:FireServer(price)

1 Like

Format the code please, will make it much easier to read `` -- to format the code, put one at the start one at the end

Just wanted to point out you could do

player.leaderstats.Points.Value -= price
1 Like

Hello, I just based my code here. If I’m going to remove “player”, how can I access the player’s leaderstats? Thank you.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEventTest")
-- Create a new part
local function onCreatePart(player, partColor, partPos)
print(player.Name .. " fired the remote event")
local newPart = Instance.new("Part")
newPart.BrickColor = partColor
newPart.Position = partPos
newPart.Parent = workspace
end
 -- Call "onCreatePart()" when the client fires the remote event
remoteEvent.OnServerEvent:Connect(onCreatePart)

Still gives me the same error "“attempt to perform arithmetic (sub) on number and Instance”

You only need to remove the player from the client side. The server will have the player that fired the event already and everything after that is whatever the client sent over.

1 Like

remove the player argument, RemoteEvent#FireServer passes player for you already

1 Like

A bit unrelated to your question, but you’re doing most of your checks on the client. Exploiters can take advantage of this. Consider changing your checks to server-side.

1 Like

I mean on this part, how can I access the player’s leaderstats?

local function BuyThisItem(price)
	player.leaderstats.Points.Value = player.leaderstats.Points.Value -price <--MY PROBLEM
end

Now it gives me this error “ServerScriptService.BuyItem:5: attempt to index nil with ‘leaderstats’” because it can’t see the player.

You pass player in to the arguments, (player, price). As stated above, FireServer automatically passed the player as the first argument.

Adding on to this point, you can use module scripts to store the price of the items and whenever the client calls the remote event, the server checks the module script for the appropriate price, checks whether the client has it, and charges the client.

2 Likes

The first user told me to remove the player argument. :confused:

The event automatically inserts the player as the first parameter

1 Like

I believe he means to remove “player” from FireServer(). In the local script it should read:

ButItem:FireServer(price)
1 Like

Thank you, it is now working well as it should be. I thought that I should delete the player argument both in the client and server argument. Now I understand. Thank you so much for your help guys. :smile:

2 Likes