Hello, I started experimenting with Remote Events. I have 3 args, the default player one, one that goes by the name of donationUser (that becomes nil), and this one donationAmount (doesn't become nil)
Server:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local donateEvent = Instance.new("RemoteEvent", ReplicatedStorage) donateEvent.Name = "DonateEvent" function checkDonation(player, donationUser, donationAmount) print(donationUser) game.Workspace.DonationUser.Value = donationUser game.Workspace.DonationUser.DonationAmount.Value = donationAmount end donateEvent.OnServerEvent:Connect(checkDonation())
Client:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local donateFunc = ReplicatedStorage:WaitForChild("DonateEvent") script.Parent.MouseButton1Click:connect(function() local donation = script.Parent.Parent donateFunc:FireServer(donation.Name.Text, donation.Amount.Text) end)
Haha I see this often, people calling the function when they were supposed to pass it in! Don't worry I only know this because I made the same mistake before...
The Confusion
Functions can be passed in as arguments into other functions along with values (sorry, the way I worded it is more confusing than what it actually is), a cheap example:
local function myFunc(f, x) -- This function (myFunc) calls another function f(x) -- f here would be a function and x would be the only parameter end -- Define the callback local function f(x) print("Function (f) was called with argument (" .. x .. ")!") end f(5) -- "Calling" function (f) myFunc(f, 5) -- Using another function (myFunc) to call function (f)
"Hey, that seems pretty useless."
Sure, in this scenario it looks and probably is very useless.
An easy example would be events, which you are already using so lets go with it.
Snippet of your code:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local donateEvent = Instance.new("RemoteEvent", ReplicatedStorage) donateEvent.Name = "DonateEvent" function checkDonation(player, donationUser, donationAmount) print(donationUser) game.Workspace.DonationUser.Value = donationUser game.Workspace.DonationUser.DonationAmount.Value = donationAmount end donateEvent.OnServerEvent:Connect( checkDonation() ) -- This part
Here to Connect(...)
the OnServerEvent event a function is needed, you might have thought you passed in a function but you actually called the function and passed in whatever it returned which was nil
.
local function f(x) return x ^ 2 + 2 * x + 1 -- Just a random expression end local a = f(5) -- a = 36 local b = f -- b = f
Look at the difference when you call a function with parentheses and when you don't? Well now you know.
So the event never fired the function, but it was instead called with no arguments, the same thing as passing in nil for every argument. Now you know what's causing the problem, and now for the part everyone was waiting for...
Solution
TL;DR Just remove the parentheses next to checkDonation
below, ¯\_(insert japanese word here)_/¯.
There's a good chance I made a mistake or have it all completely wrong, just reply with a comment so I can figure out the real problem.