Communicating with a Script of the Same Parent

Alright, so, I have 2 scripts with the same general parent (an NPC character.) One of them handles the animations of the character (AnimationScript), and the other is detecting when the ProximityPrompt gets triggered (PromptScript).
image

What I want to happen is when the ProximityPrompt gets triggered, the PromptScript tells the AnimationScript to run an animation.

However, I would like to know the best way to do it is. I want a clean, efficient way to handle this without anything messy.

I know that you have to use BindableEvents to do it, but if I do, would I use the same BindableEvent for every NPC I am going to create which needs 2 scripts to communicate with each other? Help!

Use a BindableEvent or BindableFunction. There should be a copy of the event or function for each unique thing that can call it, don’t try and share them! I.e. there should be one “StartAnimationEvent” BindableEvent for each NPC. That does not necessarily mean you need copies of the scripts for each one, but that’s the easy way to do it.

1 Like

I want to have many NPC’s in the game, though, and having like 20 bindableevents for the same purpose seems a bit messy. Are there any other alternatives?

You shouldn’t share them because they are guaranteed to fire in-order. The more events you put into the same one, the longer the queue gets and the worse the performance becomes. This is even more important with Remotes. Also, if you share them, you will have to pass some value to indicate which NPC needs handled, which is easy to mess up since an event might arrive after the NPC is removed. It is also just error prone when writing. If you use a different bindable for each NPC, then you only need to look at that bindable’s parent. In that case, if the NPC has been deleted, the event cannot fire because it’s gone, so you don’t have to worry about that either.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.