Hello, could someone explain in what cases to use this event and why did it be used? please
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)
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)
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)