Deeper understanding into :Connect(function(Something)

So…

Touched would be :Connect(function(Touched) ?

or ChildAdded would be :Connect(function(ChildAdded) ?

Don’t worry. At your stage, you don’t really have to use variadic functions as they’re not that useful than you might think. It’s better to use arrays as an alternative.

1 Like

Oh wait are the parameters on the left side?

No, you call Connect from the event. You receive parameters in function parentheses. I and others have provided some examples of how to use it in our previous posts. You can scroll up and look at them.

:sweat_smile: I dont know what arrays do either…

Sorry for still being dumbfounded but…

I couldnt find them as good examples because I wouldn’t know what var1 or var2 do. So far from my knowledge I can just go to the Objects tab and look at the function and look at the left tab for the parameters?

Functions are actually easy to understand if you KNOW how to understand them.

function test(arg1, arg2)
    print(arg1)
end

Arg1 and Arg2 are arguments passed through the function which would act the same as a variable- but eliminates a line.

When calling the function, you do need to pass the arguments, like this:

test(player.Name, hello) – That would pass the functions, player.Name being argument 1 and hello being argument 2.


So essentially, it’s just a variable created inside of the function, to reduce lines of code. (They can also only be accessed within the function body.)

Did you read all the posts that people have posted here?

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