How to use BindableEvent

Hello, could someone explain in what cases to use this event and why did it be used? please :pray:

code from docs:

-- Create or get reference to a BindableEvent
local beGoal = Instance.new("BindableEvent")
 
-- Connect a function to the custom event
local function onGoalScored(team)
	team.Score = team.Score + 1
end
 
beGoal.Event:Connect(onGoalScored)
 
-- Somewhere else in your code, fire the event
local Teams = game:GetService("Teams")
local redTeam = Teams["Red Team"]
beGoal:Fire(redTeam)
1 Like

Bindable Events can be used to connect multiple scripts. For example:

-- Create or get reference to a BindableEvent
local beGoal = Instance.new("BindableEvent")
 
-- Connect a function to the custom event
local function onGoalScored(team)
	team.Score = team.Score + 1
end
 
beGoal.Event:Connect(onGoalScored)
 
-- Somewhere else in your code, fire the event
local Teams = game:GetService("Teams")
local redTeam = Teams["Red Team"]
beGoal:Fire(redTeam)

You could have another script after this say.

beGoal.Event:Connect(function()
	print("Wow! I was Fired!")
end)
2 Likes

And how could multiple scripts be connected in this event? :dizzy_face:

1 Like

You would set the parent of the event and get the object using scripts. For example, if the bindable event was in ReplicatedStorage.

local BindableEvent = game:GetService("ReplicatedStorage").BindableEvent
BindableEvent.Event:Connect(function()
	print("I was fired from another script!")
end)
2 Likes

Oh :dizzy_face: thanks alot brother! :3 :#

2 Likes