Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help on making a money transfer system?

I've been trying to make a money transfer system for my game, but everything I've tried always gets this error: "Argument 1 missing or nil." This is the script firing the remote event.

receiver = script.Parent.Receiver.Text
amount = script.Parent.Amount2Send.Text
amount2send = tonumber(amount)
sendButton = script.Parent.Send
plrName = game.Players.LocalPlayer.UserId
sendButton.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.BankTransfer:FireServer(amount2send, receiver, plrName)
end)

script.Parent.Close.MouseButton1Click:Connect(function()
	script.Parent.Visible = false
end)

This is the serverscript.

game.ReplicatedStorage.BankTransfer.OnServerEvent:Connect(function(amount2send, receiver, plrName, plr)
	local sender = plrName
	local receiverID = game.Players:GetPlayerByUserId(receiver)
	local amount = amount2send
	if plr.Wallet.Value >= amount then
		plr.Wallet.Value = plr.Wallet.Value - amount
		receiverID.Wallet.Value = receiverID.Wallet.Value + amount
	end
end)

1 answer

One issue I noticed right of the bat is .OnServerEvent's first argument is the player instance for the player that fired the event.

RemoteEvent API:

RBXScriptSignal OnServerEvent ( Instance player , Tuple arguments )

Another thing is that if the string cannot be converted to a number, tonumber(string) will return nil, so I would add a check to make sure amount2send is not nil.

game.ReplicatedStorage.BankTransfer.OnServerEvent:Connect(function(plr, amount2send, receiver, plrName) -- changed the order of the arguments
	if amount2send == nil then warn("Amount is nil") return end -- Exit the function to prevent error
    local sender = plrName
    local receiverID = game.Players:GetPlayerByUserId(receiver)
    local amount = amount2send
    if plr.Wallet.Value >= amount then
        plr.Wallet.Value = plr.Wallet.Value - amount
        receiverID.Wallet.Value = receiverID.Wallet.Value + amount
    end
end)

If you need further assistance, feel free to contact me on Discord (phxntxsmic#2021).

This discussion is preserved as it was. New answers, comments, and votes are closed.