เรียนรู้
Engine Class
ContextActionService
ไม่สามารถสร้าง
บริการ

*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่


สรุป
วิธีการ
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):()
เลิกใช้แล้ว
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):()
เหตุการณ์
สมาชิกที่ได้รับทอด
ตัวอย่างโค้ด
เครื่องมือ 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("กำลังรีโหลด!")
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)

เอกสารอ้างอิงเกี่ยวกับ API
วิธีการ
BindAction
ความสามารถ: Input
ContextActionService:BindAction(
actionName:string, functionToBind:function, createTouchButton:boolean, inputTypes:Tuple
):()
พารามิเตอร์
actionName:string
functionToBind:function
createTouchButton:boolean
inputTypes:Tuple
ส่งค่ากลับ
()
ตัวอย่างโค้ด
เครื่องมือ 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("กำลังรีโหลด!")
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)
ตัวจัดการการกระทำทั่วไป
local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("กำลังจัดการการกระทำ: " .. actionName)
print(inputObj.UserInputType)
end
-- เนื่องจากฟังก์ชันนี้ไม่คืนค่าหมายความว่า ตัวจัดการนี้จะ
-- "จม" การป้อนข้อมูลและตัวจัดการการกระทำอื่น ๆ จะไม่ถูกเรียกหลัง
-- ตัวนี้.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)
ตัวจัดการการกระทำซ้อนกัน
local ContextActionService = game:GetService("ContextActionService")
-- กำหนดฟังก์ชันจัดการสำหรับ FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("ฟังก์ชันจัดการการกระทำหนึ่ง: " .. actionName)
end
-- ฟังก์ชันจัดการการกระทำนี้คืนค่า nil ดังนั้นจึงถือว่ามัน
-- จัดการการกระทำอย่างถูกต้อง.
end
-- เชื่อมโยงการกระทำ FirstAction (มันอยู่ที่ด้านล่างของสแต็ก)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- กำหนดฟังก์ชันจัดการสำหรับ SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("ฟังก์ชันจัดการการกระทำสอง: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- การคืนค่า nil จะทำให้ Sink inputs
return Enum.ContextActionResult.Sink
end
end
-- เชื่อมโยง SecondAction เหนือการกระทำแรก (เนื่องจากมันถูกเชื่อมโยงเมื่อไม่นานมานี้ มันจึงอยู่ที่ด้านบนของสแต็ก)
-- หมายเหตุว่าการกระทำที่สองใช้ปุ่มเดียวกันกับ
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority
ความสามารถ: Input
ContextActionService:BindActionAtPriority(
actionName:string, functionToBind:function, createTouchButton:boolean, priorityLevel:number, inputTypes:Tuple
):()
พารามิเตอร์
actionName:string
functionToBind:function
createTouchButton:boolean
priorityLevel:number
inputTypes:Tuple
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ลำดับความสำคัญของการผูกการกระทำของ 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(`การกระทำ [{actionName}] เกิดขึ้น รหัสคีย์ [{inputObject.KeyCode}] ถูกกด.`)
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(`การกระทำ [{actionName}] เกิดขึ้น รหัสคีย์ [{inputObject.KeyCode}] ถูกกด.`)
return Enum.ContextActionResult.Sink
end
-- โดยไม่มีการกำหนดความสำคัญ การกระทำที่ถูกผูกล่าสุดจะถูกเรียกก่อน,
-- ดังนั้นการกด INPUT_KEY1 จะพิมพ์ "Punch" และจากนั้นจะทำการซื่งข้อมูล.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- ที่นี่เราผูกทั้งสองฟังก์ชันในลำดับเดียวกันกับด้านบน แต่โดยมีการสลับความสำคัญอย่างชัดเจน.
-- กล่าวคือ เรามอบความสำคัญที่สูงกว่า 2 ให้กับ "Throw" ดังนั้นมันจะถูกเรียกก่อน,
-- แม้ว่า "Punch" จะถูกผูกเมื่อเร็ว ๆ นี้.
-- การกด INPUT_KEY2 จะพิมพ์ "Throw" และจากนั้นจะทำการซื่งข้อมูล.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActionToInputTypes
เลิกใช้แล้ว

BindActivate
ความสามารถ: Input
ContextActionService:BindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodesForActivation:Tuple
):()
พารามิเตอร์
userInputTypeForActivation:Enum.UserInputType
keyCodesForActivation:Tuple
ส่งค่ากลับ
()

GetAllBoundActionInfo
ความสามารถ: Input
ContextActionService:GetAllBoundActionInfo():Dictionary
ส่งค่ากลับ

GetBoundActionInfo
ความสามารถ: Input
ContextActionService:GetBoundActionInfo(actionName:string):Dictionary
พารามิเตอร์
actionName:string
ส่งค่ากลับ

GetButton
ผลตอบแทน
ความสามารถ: Input
ContextActionService:GetButton(actionName:string):Instance
พารามิเตอร์
actionName:string
ส่งค่ากลับ

GetCurrentLocalToolIcon
ความสามารถ: Input
ContextActionService:GetCurrentLocalToolIcon():string
ส่งค่ากลับ

SetDescription
ความสามารถ: Input
ContextActionService:SetDescription(
actionName:string, description:string
):()
พารามิเตอร์
actionName:string
description:string
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
end

SetImage
ความสามารถ: Input
ContextActionService:SetImage(
actionName:string, image:string
):()
พารามิเตอร์
actionName:string
image:string
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
end

SetPosition
ความสามารถ: Input
ContextActionService:SetPosition(
actionName:string, position:UDim2
):()
พารามิเตอร์
actionName:string
position:UDim2
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
end

SetTitle
ความสามารถ: Input
ContextActionService:SetTitle(
actionName:string, title:string
):()
พารามิเตอร์
actionName:string
title:string
ส่งค่ากลับ
()
ตัวอย่างโค้ด
ปุ่มแตะ ContextActionService
local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- รูปภาพของฟองคำพูดที่มี ? อยู่ในนั้น
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("กำลังตรวจสอบ")
end
end
-- สำหรับอุปกรณ์สัมผัส ปุ่มจะถูกสร้างขึ้นบนหน้าจอโดยอัตโนมัติโดยพารามิเตอร์ที่ 3
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- เราสามารถใช้ฟังก์ชันเหล่านี้ในการปรับแต่งปุ่ม:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "ดู")
ContextActionService:SetDescription(ACTION_INSPECT, "ตรวจสอบบางสิ่ง.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- เราสามารถจัดการกับปุ่มโดยตรงโดยใช้ ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- จำไว้ว่า: อุปกรณ์ที่ไม่สัมผัสจะไม่คืนค่าอะไร!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- ทำให้ ImageButton เป็นสีเขียว
end

UnbindAction
ความสามารถ: Input
ContextActionService:UnbindAction(actionName:string):()
พารามิเตอร์
actionName:string
ส่งค่ากลับ
()
ตัวอย่างโค้ด
เครื่องมือ 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("กำลังรีโหลด!")
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
ความสามารถ: Input
ContextActionService:UnbindActivate(
userInputTypeForActivation:Enum.UserInputType, keyCodeForActivation:Enum.KeyCode
):()
พารามิเตอร์
userInputTypeForActivation:Enum.UserInputType
keyCodeForActivation:Enum.KeyCode
ค่าเริ่มต้น: "Unknown"
ส่งค่ากลับ
()

UnbindAllActions
ความสามารถ: Input
ContextActionService:UnbindAllActions():()
ส่งค่ากลับ
()

เหตุการณ์
LocalToolEquipped
ความสามารถ: Input
ContextActionService.LocalToolEquipped(toolEquipped:Instance):RBXScriptSignal
พารามิเตอร์
toolEquipped:Instance

LocalToolUnequipped
ความสามารถ: Input
ContextActionService.LocalToolUnequipped(toolUnequipped:Instance):RBXScriptSignal
พารามิเตอร์
toolUnequipped:Instance

©2026 Roblox Corporation. Roblox, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ