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

EventListeners in functions?

Asked by 10 years ago

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

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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.

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 boombome 5 — 10y
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. BlueTaslem 18071 — 10y
0
Color me enlightened, then. I've used the Changed event for them in the past, or could have sworn I did at least. adark 5487 — 10y
Ad

Answer this question