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