PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Fire

Calling this method will fire the “Event” event. This function does not yield, even no script has connected to the “Event” event and even if a connected function yields. There are limitations on the values that can be sent as arguments; see the code samples

Parameters

Name Type Default Description

Returns

Return Type Summary

Code Samples


BindableEvent Theading Behavior

This code sample demonstrates how BindableEvents, like all Events, create threads of each connected function. Even if one errors, like ohNo does, the others continue. In addition, this sample shows how functions connected to the event can yield without blocking the thread that fired the event.

Test this code by pasting it into a Script within a BindableEvent parented to the Workspace or ServerScriptService. When running it, you’ll notice the error by ohNo does not stop the script from continuing (“Let’s-a-go!”) or any other connected function from being called. count and alphabet both do their tasks at the same time.

local be = script.Parent

-- Print numbers 1 through `n`
local function count(n)
	for i = 1, n do
		wait(.25)
		print(i)
	end
end

-- Print the first `n` letters of the alphabet
local function alphabet(n)
	for i = 0, n-1 do
		wait(.333)
		print(string.char(65+i))
	end
end

-- This function errors! But it doesn't stop
-- the others from being called
local function ohNo(n)
	if n > 10 then
		error("Oh no!")
	end
end

be.Event:Connect(count)
be.Event:Connect(alphabet)
be.Event:Connect(ohNo)
be:Fire(26)
print("Let's-a-go!")

BindableEvent Valid Values

This code sample shows the kinds of values that can be sent when firing BindableEvents using Fire. Test this code by placing a Script within a BindableEvent inside the Workspace or ServerScriptService. It will raise errors on the values that cannot be fired.

local be = script.Parent

-- Define a simple function to connect
-- to the custom event
local function onEvent(...)
	print(...)
end
be.Event:Connect(onEvent)

-- These values CAN be sent when firing BindableEvents
be:Fire()           -- nil
be:Fire(25)         -- numbers
be:Fire("hello")    -- strings
be:Fire(true)       -- booleans
be:Fire("buy", 25)  -- multiple values are OK
be:Fire{1, 2, 3}    -- tables as arrays with no gaps
                      -- note the curly braces
be:Fire{            -- tables with string keys only
	hello = "world";
	goodbye = "world";
}
be:Fire{            -- tables with string keys
	point = {1, 2};   -- whose values are also valid
	point2 = {3, 4};
}
be:Fire{            -- tables as arrays
	{1, 2, 3};        -- whose values also are valid
	{hello = "world";};
}

-- These are some values you CANNOT send to/from BindableFunctions
be:Fire{1, nil, 3}  -- tables as arrays cannot have nil gaps
be:Fire{
	[{}] = "hello";   -- table keys can only be numbers OR strings
}
be:Fire{            -- tables keys cannot be BOTH numbers AND strings  
	[1] = "apple";
	hello = "world";
}