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

Would anyone care to explain how function parameters work?

Asked by 5 years ago
Edited 5 years ago

I kind of understood them while learning python, but now I totally forget how they work and why.

function hi(x)
    y = x.Backpack
    z = script.Parent.Parent.Parent["Cup"]
    z:Clone().Parent = y    
end

script.Parent.ClickDetector.MouseClick:connect(hi)

This example script does something arbitrary, but the thing I want to focus on is the part where is says

function hi(x)

Does the part at the bottom of the script where it connects MouseClick to the function automatically describe "x" as the character? Or am I missing something entirely?

1 answer

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

When a function is connected to an event, the event may pass certain parameters. Mind you, you don't just know what these parameters are; you'll have to look at the wiki when you aren't sure of one. For example, the PlayerAdded event, when connected to a function, passes the parameter of the player that joined the game.

game:GetService("Players").PlayerAdded:Connect(function(player)
    print(player.Name) -->their name
end)

--Similarly with the **MouseClick** event of ClickDetectors, it passes the player that clicked.

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    print(player.Name) --note that MouseClick should only be used on the server
end)

You can, of course, leave the parameters out if you deem them unnecessary for your situation. Additionally, they're just variables. This means you can call them whatever you want. In the above example, I referred to them as player because that's obviously the most reasonable name. I could have called it x, though, like you have done. Anyways, whenever you aren't sure of the parameter of an event, just check it on the wiki.

0
what the heck is the website doing to my answer Gey4Jesus69 2705 — 5y
0
Oh okay, so it only passes certain parameters. I was worried they would be arbitrary. Thanks! MustangHeart 67 — 5y
Ad

Answer this question