Remote event help

Hi, I have hit a curb nearing the completion of a peer-to-peer currency transfer script, and would like some help with this error I can’t figure out how to resolve.

My currency script has 2 parts.

  • The StarterGUI which provides the interface paired with a local script
  • The Serverscript which provides the currency change

The local script in the UI toggles a remote event, titled SendValues.

Heres the important snippet of the Local Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage:WaitForChild("SendValues")


	if Sender.leaderstats.Cash.Value >= (tonumber (Amount)) then

		if game.Players:FindFirstChild(Recipient) then
			print(Recipient, Sender, Amount)
			-- Invoke the function
			local sendmoney = remoteFunction:FireServer(Recipient, Sender, Amount)
			print ('Fired Server')

Initially, it prints the Recipient, Sender, and Amount correctly. According to output, this script is totally fine.

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteFunction = ReplicatedStorage:WaitForChild("SendValues")

remoteFunction.OnServerEvent:Connect(function(Sender, Recipient, Amount)
	print(Sender, Recipient, Amount)
	Sender.leaderstats.Cash.Value -= (tonumber (Amount))
	print('Removed Cash from Sender')
	game.Players:FindFirstChild(Recipient).leaderstats.Cash.Value += (tonumber (Amount))
	print('Gave Cash to Recipient')

Here, everything goes haywire.

Firstly, the values printed are incorrect. In the local script, it could print:
imaxZulu832, imaxZulu832, 12000
In the server script it prints:
imaxZulu832, imaxZulu832, imaxZulu832.

Secondly, an error shows up.
‘attempt to perform arithmetic (sub) on number and nil’
I assume this means that the amount was replaced with the wrong value, as above.

Any help will be much appreciated, thanks.

remoteFunction.OnServerEvent:Connect(function(Sender, Recipient, Amount)

You sent 3 variables from the local script and you need to take 3, you wrote 3 but you received only 2, the first variable is always the player who fired the remote so change it to this.

remoteFunction.OnServerEvent:Connect(function(Player , Sender, Recipient, Amount)

This really helps, has solved a problem, but led to another.

I’m getting an error
attempt to index nil with ‘Cash’
There’s definitely a leaderstat called cash.
I’ll mark your post as a solution.

1 Like

Send me the whole script, ill check it

Hey there, I just want to make sure you know RemoteFunctions and RemoteEvents are two different things (your variable names confused me, you called the remoteEvent “remoteFunction”)

Also to add to the initial solution, I believe your parameters and arguments are in the wrong order.
In the local script you send the arguments in this order: Recipient, Sender, Amount
Then in the server script you receive them like: Sender Recipient, Amount

See, the recipient and sender are switched, this could be the cause of the second error aswell:

The amount was the wrong variable, and I had tried on an unsaved version doing it in the right order, but the result was the same. It turns out that there’s a 4th variable that’s called Player, and because of that it only took in Player, Sender and Reciever, not the amount. I got rid of Sender entirely for this reason.

Ah yes. The sender is already the LocalPlayer aka the player the remote event automatically gets passed. So you don’t need it. But when you had the Player and Sender parameters it made Amount nil. Makes sense.

1 Like