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

BindableFunction Admin Door

BindableFunction Admin Door

Sep 17 2018, 5:29 PM PST

This code sample demonstrates one possible usage of BindableFunction|BindableFunctions - admin doors. It also demonstrates why BindableFunctions are important, since they allow multiple scripts to communicate with each other.

The code below contains two parts:

  1. The script containing the BindableFunction/Invoke function. This is the AdminDoorManager script in the image below. It also contains the BasePart/Touched event that checks if the part that touched the door is a Player, invokes the BindableFunction if the part is a player, and processes the results of the invoke.

  2. The script containing the BindableFunction/OnInvoke callback. Since this is a callback, the Invoke() function will wait for and receive the results of the invoked function. This is the AdminManager script in the image below. It checks if the player who touched the door is in a list of admin player names. If so, it returns true and the AdminDoorManager script opens the door. If not, it returns false and the AdminDoorManager kills the player instead of opening the door.

Workspace hierarchy

-- AdminManager Script
local admins = {"Player1", "Builderman", "Telamon"}

local BindableFunction = script:FindFirstChild("IsAdmin")

BindableFunction.OnInvoke = function(player)
	for i, admin in pairs (admins) do
		if player == admin then
			return true
		end
	end

	return false
end



-- AdminDoorManager Script
local BindableFunction = game.Workspace.AdminManager.IsAdmin
local door = script.Parent

local function openDoor()
	door.Transparency = 0.8
	door.CanCollide = false
	
	wait(2)
	
	door.Transparency = 0
	door.CanCollide = true
end

local function isHumanoid(part)
	return part.Parent:FindFirstChild("Humanoid", true) 
end

door.Touched:Connect(function(part)
	local humanoid = isHumanoid(part)
	
	if humanoid then
		local player = humanoid.Parent
		local isAdmin = BindableFunction:Invoke(player.Name)
	
		if isAdmin then
			openDoor()
		else
			humanoid.Health = 0
		end
	end
end)