Help On BindableEvent

Hello!

While developing my game. I had to use communication between scripts.
Just for context:

  1. The two scripts are in StartGUI(PlayerGUI too I guess).
  2. The first script is a normal script.
  3. The second script is a LocalScript.
  4. I am doing this since in the script when a function is called it changes CameraSubject (which you can only do by LocalScripts).

So since this is my first time using BindableFunctions. Here’s an example of my code:

Script:

local BindableEvent =  --Finding it
function Testing()
BindableEvent:Fire()
print("It has been fired!")
--Other code
end

wait(5)
Testing()

LocalScript:

local BindableEvent =  --Finding it

function CameraChange()
--code here
end

BindableEvent.Event:Connect(CameraChange)

Now here’s my questions:

  • Did I do it right?
  • Is there another way to do this?
  • Is there a better way to do this?

I wanted to ask the forum before I do anything just to know if I’m on the right path.

Thanks for reading.

for script to localscript you need to use remote events and remote functions.

1 Like

BindableEvent is for communication between two of the same scripts (For example, two server scripts, or two local scripts)

You want to use a RemoteEvent.

1 Like

@Ae_xxie @PostVivic, so should I place the remote event in StarterGUI (same Parent as the two scripts) or ReplicatedStorage?

So when the script fires the RemoteEvent, the local script should execute the function.

I cannot change the normal script’s Parent due to many reasons

put it in replicatedstorage, I always have a folder called “Remotes” just to organize, If you want to fire one client then use fireclient with the player as the first parameter, If you want to yield until the client gives a response then use a remote function.

1 Like

ReplicatedStorage is the best option.

The reason behind this is because both server scripts, and local scripts can access this. That way you don’t have any problems with where you are putting things, and its more organized because ReplicatedStorage is practically designed for remote events.

1 Like

Thank you! I will try this later today and comeback with some answers.

2 Likes

Bindable events are used to communicate between 2 similar type of scripts like serverscript to serverscript,but wont work in server → local.I preffer using remote events or remote functions which only works server-> local or local-> server.

Replicated storage i preffer,most convinenet you can place it in many places toh.

@Ae_xxie @PostVivic @adcrs
Preview of code:

(RemoteEvent is in a folder in ReplicatedStorage)

Both scripts are in StarterGUI

Script:

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local Event = ReplicatedStorage.RemoteFolder:WaitForChild("RemoteEvent")

function Testing()
Event:FireAllClient()
print("It has been fired!")
--More code
end

wait(5)
Testing()

LocalScript:

local ReplicatedStorage = game:WaitForChild("ReplicatedStorage")
local Event = ReplicatedStorage.RemoteFolder:WaitForChild("RemoteEvent")

function CameraChange()
--code here
end

Event.OnClientEvent:Connect(CameraChange)

Looks good to me! Test it to make sure, ofc!

1 Like

I will do it later and comeback with the results.

I’m noticing an issue with the code here. You’re using Event:FireClient() but nowhere did you use the Player as the first parameter so that the server knows which client to trigger the event on.

You can use FireAllClients() without a Player argument. But this will trigger every client, so if you have data you don’t want a specific client to get access to it’s best to use a Player parameter using the Player Instance as the argument.

1 Like

My game is a single player game (FNAF fan game). Basically the script is one of the characters and when you die a jumpscare happens (obviously). So it doesn’t matter much.

Whether there is only one player or not, you still need a player instance as the first argument of :FireClient(), regardless.

1 Like

It worked! Hurray. Thanks for helping me everyone. I avoided a lot of time of confusion.