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

EventListeners in functions?

so i would like to instantiate an event listener inside a function, yet am currently unaware of how to begin.

my guess of how to would be like this

function registerPlayer(player)
	--code
	player.Character.Torso.Changed:connect(watchPlayerLocation)
end

function watchPlayerLocation(CFrame)
	--code
end	

is this how i would set it up?

1 answer

You're correct in theory, but the Changed event does not fire when an object's Position changes.

If you want to have a function update based on Player positions, use the RenderStepped event of the RunService. Use it in a local script.

The connect method returns a value, by the way. This 'ConnectionObject' has a disconnect method (no parameters) that you can use to disconnect the function once it is no longer needed.

3 comments

0

thanks, i could see how it would really bog down a server, and i did not know about the disconnect method, that will help a lot

1

While this doesn't affect the answer, I want to contradict adark's statement that this was recently changed. Physics / UI based positional updates have never fired Changed events.

0

Color me enlightened, then. I've used the Changed event for them in the past, or could have sworn I did at least.

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