Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Remote Event, one of 2 args become NIL and one not?

Asked by
NewGPU 36
5 years ago

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)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
It was supposed to be a shrug and I had to use double backslash \\ to escape lol GoldAngelInDisguise 297 — 5y
0
You mean like this? donateEvent.OnServerEvent:Connect(checkDonation) NewGPU 36 — 5y
0
Precisely. GoldAngelInDisguise 297 — 5y
0
It still returned NIL, with the error "bad argument #3 to Value (string expected, got nil)" NewGPU 36 — 5y
View all comments (6 more)
0
Try printing donation.Name.Text and donation.Amount.Text on the client GoldAngelInDisguise 297 — 5y
0
They return: nil and Amount. Strange, donation.Name.Text does exist and does have a value. NewGPU 36 — 5y
0
Could it be that it is interfering with the property Name? NewGPU 36 — 5y
0
Yep that was the issue ^ NewGPU 36 — 5y
0
Good job lol GoldAngelInDisguise 297 — 5y
0
donation:FindFirstChild("Name") should find the child instead of the property GoldAngelInDisguise 297 — 5y
Ad

Answer this question