How to use parameters? When to use them and when not to use them?

I’m trying to find how parameters work. And why it’s important to use them and when not to use them.

I’m confused about using functions and Events with their parameters and arguments. Why is their no argument for the Touched Event? Just the Parameter hit. Also, why do I have to use a hit? Why are parameters useful? How does the script know the parameters without the argument for the Event? This might be a lot, so no worries if you can’t answer them all.

image image

I have tried to find the answer but couldn’t. If you know the answer plz tell me. I’ve been stuck on this. Also, can you give me an example of this? :confused::question:

7 Likes

A parameter is what you send from your argument to a function and I suggest instead of checking if hit.Parent.Name is excisting, to either use:

game.Workspace.Part.Touched:Connect(function(hit)
    if hit.Parent then
        print(hit.Parent.Name)
    end
end)

or

game.Workspace.Part.Touched:Connect(function(hit)
    print(hit:GetFullName)
end)
1 Like

Some things have “invisible returns” because Roblox already privides it. For example, if the player touched a part, then roblox would have the part that touched it be a return value of the event. So, in brief terms:

Part gets touched, part is the first parameter of the touched event

Other events like PlayerAdded and PlayerRemoved have a parameter that gives the Player instance, becacuse Roblox made it so that when the player leaves, it get’s the leaving player, then fires the event.


How parameters work is as following:

local function Parameters(Parameter1, Parameter2, Parameter3) -- This would be like the touched event
    print(Parameter1, Parameter2, Parameter3) -- "a", "b", "c"
end
Parameters("a", "b", "c") -- This part is what Roblox handles, which is invisible, it returns the part it touched

Returning works like this:

local function Return()
    return "a", "b", "c"
end
local Parameter1, Parameter2, Parameter3 = Return()
print(Parameter1, Parameter2, Parameter3) -- "a", "b", "c"

If you still don’t understand, then think about it this way, there are 6 people, 3 people are getting stuff, and 3 people are receiving. The giving people give an apple to the receiving people:

G1 -> R3
G2 -> R3
G3 -> R3

That in code would look like:

local function Parameters(Parameter1, Parameter2, Parameter3) -- "Parameter1" is R1, "Parameter2" is R2, "Parameter3" is R3
    print(Parameter1, Parameter2, Parameter3) -- "a", "b", "c"
end
Parameters("a", "b", "c") -- "a" is G1, "b", is G2, "c" is G3

5 Likes

A parameter is just a variable, but then only for that function for that time, a.k.a. not for every argument you run

2 Likes
local function add(a, b)
    return a + b
end

print(add(1, 2)) -- 3

Parameters + return values allow you to design simple or complex interfaces for yourself. When used effectively, it can turn spaghetti code into an organized mess (in a good way).

Basically parameters are things you can use in your code and you can change it to whatever you want, you probably already know this from your “hello” example.

And there are events, some events can pass parameters to a function such as the case of Touched which passes the object that touched the part you specified.

There are other events that does that.

Example of events:

local clickDetector = workspace.Part.ClickDetector
local function clickFunction(playerThatClicked)
      print(playerThatClicked.Name) -- Prints the name of the player that clicked it
end
clickDetector.MouseClick:Connect(clickFunction) -- This event automatically passes the player that clicked as a argument to click function.

There are way more events that does things like that, but thats the basic for it. (Note that the parameter could be anything you want, in your case you could even rename “hit” to “wwwwlwlwl” and replace the other “hits” with that and it would still work).

Also another note is that:

clickDetector.MouseClick:Connect(function(playerThatClicked)
       print(playerThatClicked.Name)
end)
-- This does the same thing, which is printing the name of the player that clicked the clickdetector.
local function mmmmmmmmm(clicked)
      print(clicked.Name)
end
clickDetector.MouseClick:Connect(mmmmmmmmm)
2 Likes

What happens if you don’t put parameters for functions and Events? How does it affect the function or event?

Absolutely nothing. But you won’t be able to get the player through there anymore.

If you dont put parameters to functions or events they would still work the same except with the parameter.

There is a example:


clickDetector.MouseClick:Connect(function(personThatClicked)
       print("Mouse click happened")
end) 

-- This would achieve the same thing

clickDetector.MouseClick:Connect(function()
       print("Mouse click happened")
end)

The difference at them is that, the first one passes the player that clicked information when the second one doesn’t. However both of them fires when the click happens.

The same would happen to functions, if you put the parameter then you will be able to use the arguments passed to it. If you dont then you wont be allowed to use them BUT it would still fire normally.

And a note just in case you get confused about this:

You can fire a function that has parameters without arguments, however the parameter would be nil.

local function lol(name)
      print("The name is..."..name)
end

lol("HEY") -- Output: The name is...HEY
lol() -- Errors because the argument given is nil, and you cant concatenate nil.
2 Likes

I have one more question, So how are parameters useful in scripts like how can it benefit you in function and Events? Btw, Thank you for the help!!

Thank you for everyone for the Help, Much appreciate!! :smile:

It makes it really simplier in some cases, events and metamethods uses parameters for them to work and they make things really better to use.

But there are some examples of what you can do:

local function sumUp(number1, number2)
      return number1 + number2
end
-- Done! Now you can use any arguments in it and it will work
print(sumUp(1,2)) -- 3
print(sumUp(6,203)) -- 209

-- Another example of functions
local runService = game:GetService("RunService")
local function typeWriteEffect(text)
      for i = 1, #text, 1 do
            workspace.Part.SurfaceGui.TextLabel.Text = string.sub(text, 1, i)
            runService.RenderStepped:Wait()
      end
end
-- Done! Now it will make a type write effect with any text you put in there.
typeWriteEffect("Hello, Welcome to the game!")
typeWriteEffect("This might be really cool but yeah.")
typeWriteEffect("Hope you liked it!")

-- As you can notice, using the parameter makes things really easier since all you will need is change the argument text. A alternative for it would be changing variables to another value and then fire the function again, however, this consumes more time and may be useless to do it over parameters.

-- Now for events:
game.Players.PlayerAdded:Connect(function(player)
       print("Welcome to the game, "..player.Name.."!")
end)
-- This is less complicated than doing this:
local playerList = {}
game.Players.PlayerAdded:Connect(function()
       for _, player in pairs(game.Players:GetPlayers()) do
             if not table.find(playerList, player) then
                   table.insert(playerList, player)
                   print("Welcome to the game, "..player.Name.."!")
             end
       end
end)
2 Likes

Love it! Alright, Thank you for the help!! Much appreciate!!

1 Like