Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I call a function?

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

2 answers

✓ Accepted answer

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.

2 comments

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.

0

When you're making a function is it local function or just function

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.