Deeper understanding into :Connect(function(Something)

That made SO MUCH MORE SENSE. Thanks, so I can just find a function in the objects tab, click it and find its parameters?

Yes. None of them explained it that clearly.

Not necessarily- It needs to be a variable or object passed as the function argument. Functions can’t be passed within functions, as well as events, or nonexistent object(s).

Those parameters are for events. You can create your own functions by doing

local function yourFunctionName(parameter1) -- you can add more parameters, separated by a comma
    print(parameter1);
end
print(yourFunctionName(5)); -- prints 5
print(yourFunctionName("hello")); -- prints "hello"
print(yourFunctionName()); -- prints nil (parameter doesn't exist)
print(yourFunctionName(math.pi)); -- prints pi
-- etc. a parameter can be anything, as long as it will work with your function and won't produce any error
-- for example, if you make an `add` function which adds 2 parameters together,
-- then you need to make sure that all of them are numbers, and not for example strings,
-- because you can't add strings together

Actually, why not? All of them can. That’s how Connect(functionName) works:

local function passTest(par)
    local to = typeof(par);
    print(to);
    if to == "function" then
        return par(); -- if it's a function then call it and return whatever the 'par' function returns
    else
        return to; -- otherwise, if it's not a function then return the 'par' variable type
    end
end

local function test() return 1; end

print(passTest(test)); -- 1 (returned from the 'test' function)
print(passTest(workspace.Baseplate.Touched)); -- RBXScriptSignal
print(nil); -- nil
print(); -- nil

While passing a function, make sure not to call it, because you want to pass the function and not the value that the function returns. In this case, it would be a 1.

print(passTest(test)); -- ok (type: function)
print(passTest(test())); -- not ok (type: number, value: 1)

Could you give me a few examples of real scripts and explain what each parameter does? I think it will be better to show me the steps first. If possible how player is automatically connected to the player somehow?

Parameters are just very VERY new to me. Sorry!

Could you make your scripts more simple? I can’t understand most of your scripts… Sorry :sweat_smile:

1 Like

Try creating some functions on your own.
I’ll give you a few examples:

Create a function, that:

  • prints “Hello world”
  • takes 2 parameters (that are numbers), x and y, and then multiplies them together and prints the result.
  • takes 2 parameters (that are string's), str1 and str2, and then concatenates them using the .. operator, and prints the result

Try to think of some new ideas too. It will help you understand functions and events better.

I could only understand the print() part. I dont know how I can make any result with X and Y besides just printing X, Y. And strings with the … operator are still things I would like to learn later.

Where can I find more parameters?

When you call a function, you give it parameters, for example

Local x = 5

MyFunction(x)

local function MyFunction(value)
Print(value)
end

Functions are used in pretty much everything and also for cleaning code. Callbacks are like messages the functions receive

Some functions such as remoteevents might have already one parameter defined and you dont need to define it again, in this case it would be the player

[EVENT:FireServer(value) --callback 1 is player by default and value is second

Event.OnServerEvent:Connect(do)
Local function do (player, value) --But when receiving it we do need to get it because it isnt defined in the listening
–code
end

Summarizing, a parameter in the callback is what a function will either send (if you are calling it) or will receive, so that it can be used in the code.

Take this as an example: you are creating a log in system that logs you in either with a deleted account or with a real account, instead of writing the same code reading both tables you could do a “logIn” function which would work for both after coding whatever you need

Now things like events or, for example “WaitForChild” are what I call “Core” functions, they are functions that you may not see, but have been defined by engineers.

A touched event will have a callback about what it touched, because it is already defined as that by whoever worked in Roblox engineering

Yeah, but you cant pass another function within a function argument.

local Player = game.Players.LocalPlayer

local function pprint(name)
    print(name)
end

game.Players.PlayerAdded:Connect(function(player)
    pprint(player.Name) -- Thats the parameter we're passing, and in the function the "name" argument is equal to player.Name.
end)

I don’t know where you got this information from but that’s entirely false — anything by can be passed through parameters (in other languages it will have to be the same type that the function expects). Functions passed to other functions through arguments are typically called Lambda Expressions (Lua doesn’t support true lambda expression syntax ‘=>’, so you just manually construct those functions with the function keyword).

If you’re talking about your own functions, there is no set list of parameters. Defining a parameter is like defining a variable; just without the local behind it, or the = value syntax. That’s because the parameters are given their values when they are passed with the function call syntax my_function(). Within the parentheses of the call syntax, you put the arguments with each value separated by a comma.

Parameters are essentially placeholders for values that can be sent into the function. For the most part, it does not matter what the names of the parameters are, it’s primarily a matter of making sure that it describes what values you expect that function to receive.


Example 1

local function test(firstValue)
    warn("This is the function called 'test'")

    print(firstValue) -- This will print "Hello world!"
end

local function AnotherTest(completelyRandomParameterName)
    warn("This is the function called 'AnotherTest'")

    print(completelyRandomParameterName) -- This will print "Hello world!"
end

test("Hello world!")
AnotherTest("Hello world!")

In this example, both of the functions were sent the exact same value (a string that says “Hello world!”) but the names of the parameters are completely different. In this case, both of them will print the exact same value of “Hello world!” because the first parameter refers to the first value that was sent into the function (and if there was a second parameter, it would refer to the second value that was sent into the function, and so on and so forth.)

Example 2

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    print(player.Name) -- This will print the name of the player that joined
end)

---

Players.PlayerAdded:Connect(function(theFirstValueSentToThisFunction)
    print(theFirstValueSentToThisFunction.Name) -- This will still print the name of the player that joined
end)

This second example works slightly differently because we’re using events. When some events that have been connected to a function are activated, specific data will be passed through to the function. Knowing what data is sent to the function from each event comes down to experience and knowing where to research (in this case, when looking at the PlayerAdded Event Documentation page on the Roblox Developer Hub, it specifies that “player” is the only parameter. This means that the Player Instance is the only value sent to the function when the PlayerAdded event has been activated.)

Because the player Instance is the only value that is sent to a function when the PlayerAdded event is activated, generally we’ll input the word “player” as the first parameter since it makes it more clear to anyone who is reading the code what data that function is supposed to receive. If we added a second parameter to the function, like so:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player, secondParameter)
    print(player.Name) -- This will print the name of the player that joined
    print(secondParameter) -- This will print "nil" because a second value was not sent into the function
end)

Keep in mind that some events may send through multiple pieces of data, such as the Humanoid.Seated and MarketplaceService.PromptPurchaseFinished events.

Additionally, some events do not send any data to functions that they are connected to, such as the Tool.Activated and GuiButton.MouseButton1Click events. This means that any anonymous functions that they may be connected to will not need to have any parameters (as far as I’m aware).

If you have any additional questions, feel free to ask!


Additional Resources

Handling Events Developer Hub Article
Functions Developer Hub Article
Variadic Functions Developer Hub Article

6 Likes

thank you SO MUCH. this has been really helpful :slight_smile:

1 Like