Ghi chú phát hành
Học
Lớp công cụ
ContextActionService
Không Thể Tạo
Dịch Vụ

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.


Tóm Tắt
Phương Pháp
BindAction(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):()
BindActionAtPriority(actionName: string,functionToBind: function,createTouchButton: boolean,priorityLevel: number,inputTypes: Tuple):()
BindActionToInputTypes(actionName: string,functionToBind: function,createTouchButton: boolean,inputTypes: Tuple):()
Đã Lỗi Thời
BindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodesForActivation: Tuple):()
SetDescription(actionName: string,description: string):()
SetImage(actionName: string,image: string):()
SetPosition(actionName: string,position: UDim2):()
SetTitle(actionName: string,title: string):()
UnbindAction(actionName: string):()
UnbindActivate(userInputTypeForActivation: Enum.UserInputType,keyCodeForActivation: Enum.KeyCode):()
Thành viên kế thừa
Mẫu mã
Công cụ Reload dịch vụ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Đang nạp lại!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

Tài Liệu Tham Khảo API
Phương Pháp
BindAction
Các khả năng: Input
ContextActionService:BindAction(
actionName:string, functionToBind:function, createTouchButton:boolean, inputTypes:Tuple
):()
Tham Số
actionName:string
functionToBind:function
createTouchButton:boolean
inputTypes:Tuple
Lợi Nhuận
()
Mẫu mã
Công cụ Reload dịch vụ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Đang nạp lại!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)
Bộ Xử Lý Hành Động Chung
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Đang xử lý hành động: " .. actionName)
print(inputObj.UserInputType)
end
-- Vì hàm này không trả về bất cứ điều gì, bộ xử lý này sẽ
-- "chìm" đầu vào và không có bộ xử lý hành động nào khác sẽ được gọi sau
-- bộ xử lý này.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
Bộ xử lý hành động xếp chồng
local ContextActionService = game:GetService("ContextActionService")
-- Định nghĩa một bộ xử lý hành động cho **FirstAction**
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Bộ xử lý hành động một: " .. actionName)
end
-- Bộ xử lý hành động này trả về nil, vì vậy nó được giả định là
-- nó xử lý hành động một cách chính xác.
end
-- Liên kết hành động **FirstAction** (nó nằm ở dưới cùng của ngăn xếp)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Định nghĩa một bộ xử lý hành động cho **SecondAction**
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Bộ xử lý hành động hai: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Trả về nil ngầm định làm Sinking inputs
return Enum.ContextActionResult.Sink
end
end
-- Liên kết **SecondAction** lên trên hành động đầu tiên (vì nó được liên kết gần đây hơn, nó nằm ở trên cùng của ngăn xếp)
-- Lưu ý rằng **SecondAction** sử dụng cùng một phím
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority
Các khả năng: Input
ContextActionService:BindActionAtPriority(
actionName:string, functionToBind:function, createTouchButton:boolean, priorityLevel:number, inputTypes:Tuple
):()
Tham Số
actionName:string
functionToBind:function
createTouchButton:boolean
priorityLevel:number
inputTypes:Tuple
Lợi Nhuận
()
Mẫu mã
Ưu tiên BindAction của ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local INPUT_KEY1 = Enum.KeyCode.Q
local INPUT_KEY2 = Enum.KeyCode.E
local function handleThrow(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Hành động [{actionName}] đã xảy ra. KeyCode [{inputObject.KeyCode}] đã được nhấn.`)
return Enum.ContextActionResult.Sink
end
local function handlePunch(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Hành động [{actionName}] đã xảy ra. KeyCode [{inputObject.KeyCode}] đã được nhấn.`)
return Enum.ContextActionResult.Sink
end
-- Không chỉ định ưu tiên, hành động mới nhất được gắn sẽ được gọi trước,
-- vì vậy nhấn INPUT_KEY1 sẽ in "Đấm" và sau đó sink đầu vào.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Ở đây chúng ta gắn cả hai hàm theo cùng thứ tự như trên, nhưng với các ưu tiên được hoán đổi rõ ràng.
-- Nghĩa là, chúng ta cho "Ném" một ưu tiên cao hơn là 2 để nó sẽ được gọi trước,
-- mặc dù "Đấm" vẫn được gắn gần đây hơn.
-- Nhấn INPUT_KEY2 sẽ in "Ném" và sau đó sink đầu vào.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActionToInputTypes
Đã Lỗi Thời

BindActivate
Các khả năng: Input
ContextActionService:BindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodesForActivation:Tuple
):()
Tham Số
userInputTypeForActivation:Enum.UserInputType
keyCodesForActivation:Tuple
Lợi Nhuận
()

GetAllBoundActionInfo
Các khả năng: Input
ContextActionService:GetAllBoundActionInfo():Dictionary
Lợi Nhuận

GetBoundActionInfo
Các khả năng: Input
ContextActionService:GetBoundActionInfo(actionName:string):Dictionary
Tham Số
actionName:string
Lợi Nhuận

GetButton
Sinh Lợi
Các khả năng: Input
ContextActionService:GetButton(actionName:string):Instance
Tham Số
actionName:string
Lợi Nhuận

GetCurrentLocalToolIcon
Các khả năng: Input
ContextActionService:GetCurrentLocalToolIcon():string
Lợi Nhuận

SetDescription
Các khả năng: Input
ContextActionService:SetDescription(
actionName:string, description:string
):()
Tham Số
actionName:string
description:string
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nói với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng những hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
end

SetImage
Các khả năng: Input
ContextActionService:SetImage(
actionName:string, image:string
):()
Tham Số
actionName:string
image:string
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nói với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng những hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
end

SetPosition
Các khả năng: Input
ContextActionService:SetPosition(
actionName:string, position:UDim2
):()
Tham Số
actionName:string
position:UDim2
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nói với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng những hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
end

SetTitle
Các khả năng: Input
ContextActionService:SetTitle(
actionName:string, title:string
):()
Tham Số
actionName:string
title:string
Lợi Nhuận
()
Mẫu mã
Nút Chạm Dịch Vụ Hành Động Ngữ Cảnh
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Kiểm tra"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Hình ảnh của một bong bóng lời nói với ? trong đó
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Đang kiểm tra")
end
end
-- Đối với các thiết bị chạm, một nút được tạo tự động trên màn hình qua tham số thứ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- Chúng ta có thể sử dụng những hàm này để tùy chỉnh nút:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Nhìn")
ContextActionService:SetDescription(ACTION_INSPECT, "Kiểm tra một thứ nào đó.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- Chúng ta có thể thao tác trực tiếp trên nút sử dụng ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Nhớ: các thiết bị không chạm sẽ không trả về gì cả!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tô màu xanh cho ImageButton
end

UnbindAction
Các khả năng: Input
ContextActionService:UnbindAction(actionName:string):()
Tham Số
actionName:string
Lợi Nhuận
()
Mẫu mã
Công cụ Reload dịch vụ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Đang nạp lại!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

UnbindActivate
Các khả năng: Input
ContextActionService:UnbindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodeForActivation:Enum.KeyCode
):()
Tham Số
userInputTypeForActivation:Enum.UserInputType
keyCodeForActivation:Enum.KeyCode
Giá Trị Mặc Định: "Unknown"
Lợi Nhuận
()

UnbindAllActions
Các khả năng: Input
ContextActionService:UnbindAllActions():()
Lợi Nhuận
()

Sự Kiện
LocalToolEquipped
Các khả năng: Input
ContextActionService.LocalToolEquipped(toolEquipped:Instance):RBXScriptSignal
Tham Số
toolEquipped:Instance

LocalToolUnequipped
Các khả năng: Input
ContextActionService.LocalToolUnequipped(toolUnequipped:Instance):RBXScriptSignal
Tham Số
toolUnequipped:Instance

©2026 Roblox Corporation. Roblox, logo Roblox và Powering Imagination là các nhãn hiệu đã đăng ký và chưa đăng ký của chúng tôi tại Hoa Kỳ và các quốc gia khác.