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

How do I call a function?

Asked by 9 years ago

I'm kinda new to scripting and all, but how do I call functions?

2 answers

Log in to vote
2
Answered by 9 years ago

Oh, it's quite easy once you get the hang of it.

function addNumbers() --Your function
    print(5+5)
end

addNumbers() --To call the function, simply put its name, followed by two parentheses.

For events though, here's how it would work:

function touch()
print('Something was touched by something else!')
end
script.Parent.Touched:connect(touch)--Check events on the wiki for this. Script.Parent is referencing the brick that the script is child unto.
1
The distinction here is that connect, not touch, is invoked (as connect is a method). When the Touched event 'fires', the connect method will call the function passed to it when it is invoked, passing in any applicable arguments. In the case of Touched, the part that caused the event to fire. adark 5487 — 9y
0
When you're making a function is it local function or just function FancyFame 15 — 9y
Ad
Log in to vote
2
Answered by 9 years ago

You simply type the function's name and then add parenthesis.

function Function()
    print("How to call a functon")
end

Function() --Called the function

--Notice you can't name a function "function" because it's a keyword, "Function" with a capital F is ok

Some functions can be called using an event, for example the Toched event

function Function()
    printf("How to call a function using an event - example")
end

--Now you need to add the calling line which will refer to the object you want to be touched so the function will start running
--Say the script is inside the object (that you want to touch to make the function start running)

script.Parent.Touched:connect(Function)

Answer this question