how to end a function?

I have a function for scoring and I need it to end in my onCollision function

I’m not sure if that function is on an enterFrame event, a timer, or something else, so I’ll go with the most general solution.
You can use a trigger variable.

At the top of your file, add a new local variable such as
 

local stopMyFunction;  

Then, inside your function, put everything into an if statement like this
 

myFunction = function()   if not stopMyFunction then      --put all your function code here else --you can put some cleanup code here, like if the function is called from a timer, then you can put timer.cancel here, while if it's an enterFrame event, you can remove the listener   end end  

In the collision event, simply change stopMyFunction to true
 

function object:collision(event)   if event.phase == "began" then    stopMyFunction = true;   end end  

This way, everything inside your function won’t be executed (apart from the cleanup code).
Also, on sceneExit or similar, make sure to reset your  stopMyFunction back to nil or false.

I’m not sure if that function is on an enterFrame event, a timer, or something else, so I’ll go with the most general solution.
You can use a trigger variable.

At the top of your file, add a new local variable such as
 

local stopMyFunction;  

Then, inside your function, put everything into an if statement like this
 

myFunction = function()   if not stopMyFunction then      --put all your function code here else --you can put some cleanup code here, like if the function is called from a timer, then you can put timer.cancel here, while if it's an enterFrame event, you can remove the listener   end end  

In the collision event, simply change stopMyFunction to true
 

function object:collision(event)   if event.phase == "began" then    stopMyFunction = true;   end end  

This way, everything inside your function won’t be executed (apart from the cleanup code).
Also, on sceneExit or similar, make sure to reset your  stopMyFunction back to nil or false.