Problem with money sending system

Greetings,

I have some problems with my money sending system. I am not sure why does it do it, but it should send the money to the specified person, but for some reason, it sends it to the same person. Here are my scripts:

1st being a local script

script.Parent.MouseButton1Click:Connect(function(player)
	local player = game.Players.LocalPlayer
	print(player.Name.. " is the sender")
	local sender = player.Name
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local receiver = script.Parent.Parent.Player.Text
	local amount = script.Parent.Parent.Amount.Text
	local amountvalue = script.Parent.Amount.Value
	amountvalue = amount
	local rs = game:GetService("ReplicatedStorage")
	local re = rs:WaitForChild("Sending"):WaitForChild("Send")
	local percent = amountvalue / 20
	local finalamount = amount - percent

	if game.Players:FindFirstChild(receiver) then
		print("Amount is: " ..amountvalue)
		print("Final amount is: " ..finalamount)
		print("Receiver is: " ..receiver)
		re:FireServer(amount, receiver, sender, finalamount, cash, amountvalue) 
		print("Remote event for sending has been triggered.")
	elseif not game.Players:FindFirstChild(receiver) then
		print("Receiver " ..receiver.. " not found.")
	end
end)

2nd being a server script located in serverscriptservice

local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("Sending"):WaitForChild("Send")
local players = game:GetService("Players")

re.OnServerEvent:Connect(function(localPlayer, sender, amount, receiver, finalamount, cash, amountvalue)
	print("Remote event for sending has been received.")
	print(sender, amountvalue, receiver, finalamount)
	localPlayer:WaitForChild("leaderstats"):WaitForChild("Cash").Value = localPlayer:WaitForChild("leaderstats"):WaitForChild("Cash").Value - amountvalue
	players:WaitForChild(receiver):WaitForChild("leaderstats"):WaitForChild("Cash").Value = players:WaitForChild(receiver):WaitForChild("leaderstats"):WaitForChild("Cash").Value + finalamount
	print("Transaction completed.")
end)

I used a remote event to connect the 2 scripts. The problem seems to occur in the second script at the following line players:WaitForChild(receiver):WaitForChild("leaderstats"):WaitForChild("Cash").Value = players:WaitForChild(receiver):WaitForChild("leaderstats"):WaitForChild("Cash").Value + finalamount

Please help me, I don’t understand where I may be wrong. Thanks!

Make sure the parameters match up. You have it as:

re:FireServer(amount, receiver, sender, finalamount, cash, amountvalue)

and

:Connect(function(localPlayer, sender, amount, receiver, finalamount, cash, amountvalue)

Try switching the one in the server script to:

:Connect(function(localPlayer, amount, receiver, sender, finalamount, cash, amountvalue)

Also, sending the “sender” value again is pointless

I’ll optimize your script for you, with the fixes included:

script.Parent.MouseButton1Click:Connect(function(player)
	local player = game.Players.LocalPlayer
	print(player.Name.. " is the sender")
	local sender = player.Name
	local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash")
	local receiver = script.Parent.Parent.Player.Text
	local amount = script.Parent.Parent.Amount.Text
	local amountValue = script.Parent.Amount.Value
	amountValue = amount
	local rs = game:GetService("ReplicatedStorage")
	local re = rs:WaitForChild("Sending"):WaitForChild("Send")
	local percent = amountvalue / 20
	local finalamount = amount - percent

	if game.Players:FindFirstChild(receiver) then
		print("Amount is: " ..amountValue)
		print("Final amount is: " ..finalAmount)
		print("Receiver is: " ..receiver)
		re:FireServer(amount, receiver, finalAmount, cash, amountValue) 
		print("Remote event for sending has been triggered.")
	elseif not game.Players:FindFirstChild(receiver) then
		print("Receiver " ..receiver.. " not found.")
	end
end)
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("Sending"):WaitForChild("Send")
local players = game:GetService("Players")

re.OnServerEvent:Connect(function(localPlayer, amount, receiver, finalAmount, cash, amountValue)
	print("Remote event for sending has been received.")
	print(localPlayer.Name, amountValue, receiver, finalAmount)
	localPlayer:WaitForChild("leaderstats"):WaitForChild("Cash").Value -= amountValue
	players[receiver]:WaitForChild("leaderstats"):WaitForChild("Cash").Value += finalAmount
	print("Transaction completed.")
end)

It works well with replacing as you said before editing. Thank you!

If I encounter any problem with those scripts (I will do a couple of modifications to prevent exploits or errors), can I please contact you? Thank you once again!

Sure!

Make sure to mark my response as the solution