Passing integers from the CLIENT to SERVER

I need help with something i am making but in order to continue i need help because i need to pass numbers from the client to the server and my code wont work

Use RemoteEvents to pass values from the client to server and vice versa.
The most common place to store them is somewhere in ReplicatedStorage.

If you scroll down on this post you will find the Client to Server section.

2 Likes

You should probably provide the code in question, without it we can’t really offer you any assistance.

1 Like
for _, Gate in pairs(Gates) do
	local Price = Gate.Price
	local GateN = Gate.ObjectName.Value
	local button = Gate
	Gate.MouseButton1Click:Connect(function()
		if leaderstats.Coins.Value >= Price.Value then
			PayGate:FireServer(Price)
		end
	end)
end

Not sure if these are correct

Looks fine, now let’s see the ServerScript

local function handlePayment(player ,payment)
	local paymentValue = payment
	player.leaderstats.Coins.Value -= paymentValue
end



PayGate.OnServerEvent:Connect(handlePayment)

This is the cause

ServerScriptService.PayGate:9: attempt to perform arithmetic (sub) on number and Instance

Don’t handle the payment on the client, since they can easily just manipulate the price. Instead, you should have the server find which gate the client is attempting to purchase which can be done by checking if the player is near the specified gate, then ON THE SERVER you should check if the client is able to purchase the gate and if they can, unlock the gate for them.

is the last bit through the client or not

plus im using GUI buttons

im also wanting to make it destroyed after paying for it only for that player

for _, Gate in pairs(Gates) do
	local Price = Gate.Price
	local GateN = Gate.ObjectName.Value
	local button = Gate
	Gate.MouseButton1Click:Connect(function()
		PayGate:FireServer(Price.Value) -- I assume 'Price' is an instance.
	end)
end

local function handlePayment(player ,payment)
    --do the sanity checks here, on the server.
    if not tonumber(payment) then return end
	player.leaderstats.Coins.Value -= payment
end



PayGate.OnServerEvent:Connect(handlePayment)
2 Likes

it still allows me to pay the gate even if it under the amount

Be sure that exploiters dont pass negative numbers.

3 Likes

Do the checks on server. And if they have enough of that amount - open the gate.
Also check the type of the passed value to make sure it is a number and not other values / nan.

1 Like

ok thank you and if you are wondering about my projects it’s a simulator gate/door but I’m making 1 script for all doors

1 Like