工具是一個 Humanoid 對象可以裝備的對象。對玩家來說,它們被儲存在一個 Backpack 對象上,該對象與一個 Player 對象相關。在遊戲中,玩家可能會有多個工具,這些工具會在畫面底部出現為圖示。裝備工具將它從背包移至 Player.Character 模型中的 Workspace 模型。預設情況下,工具會被保持在右手中,並擁有一個名為「處理」的手把,其中內部有一個 Part 名為「處理」的物件(雖然如果 Tool.RequiresHandle 關閉,就不需要一個手把)。將提供給重生玩家的工具應該存儲在 StarterPack 中。
在桌面上,按下數字鍵(1、2、3……)會裝備一個工具。裝備的工具可以透過按下「返回鍵」到工作區中掉落。建議您關閉Tool.CanBeDropped以防止掉落工具、死亡、重生並再次掉落工具以複製工具。在手把戲控制器上,LB和RB按鈕會裝備工具。您可以通過設置 Tool.ManualActivationOnly 關閉激活來禁用左鍵點擊(或遊戲手柄上的右觸發器)。這樣做需要您通過某種其他使用者輸入來啟用自己。
工具不是唯一的方法來捕捉使用者輸入。您也可以使用 ContextActionService , UserInputService 或 Player:GetMouse() .如果您需要一個工具具有多個操作,例如在工具配備時按下一個鍵,您應該在 BindAction 和 UnbindAction 事件中使用 ContextActionService 的 Equipped 和 Unequipped,分別使用。使用 LocalScript 將這些操作通過工具內的 RemoteFunction 傳送到服務器。
範例程式碼
This code is meant to be placed in a Script within a Tool. It allows a player to spawn explosions by equipping the tool and clicking on the ground. It does so by defining a function, explode, which creates a non-deadly explosion at a given point. Then, it defines a function, onActivated, that runs when the tool is activated. Finally, it connects the Activated event of the tool to the onActivated function.
To test this code out, try creating a Tool and put a Part inside it. Name the Part "Handle". Put a Script inside the Tool next, and paste the code into it. Finally, put the Tool in the StarterPack.
local tool = script.Parent
local function explode(point)
local e = Instance.new("Explosion")
e.DestroyJointRadiusPercent = 0 -- Make the explosion non-deadly
e.Position = point
e.Parent = workspace
end
local function onActivated()
-- Get the Humanoid that Activated the tool
local human = tool.Parent.Humanoid
-- Call explode with the current point the Humanoid is targetting
explode(human.TargetPoint)
end
tool.Activated:Connect(onActivated)
This code sample is for a Tool object with a Part named Handle. It detects when Humanoids other than the current holder hit the handle, and deals some damage to them. In addition, when the Tool is activated, it triggers a slash animation in the default character animation scripts. Try out this script by creating a Tool object in the StarterPack. Put a Part inside it, and name it Handle. Paste this code into a Script inside the Tool, then try slashing at another Humanoid!
local tool = script.Parent
local function onTouch(partOther)
-- First, try to see if the part we touched was part of a Humanoid
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
-- Ignore touches by non-humanoids
if not humanOther then
return
end
-- Ignore touches by the Humanoid carrying the sword
if humanOther.Parent == tool.Parent then
return
end
humanOther:TakeDamage(5)
end
-- Trigger a slash animation
local function slash()
-- Default character scripts will listen for a "toolanim" StringValue
local value = Instance.new("StringValue")
value.Name = "toolanim"
value.Value = "Slash" -- try also: Lunge
value.Parent = tool
end
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
概要
屬性
控制玩家是否可以放下工具。
關於工具是否可以使用。
將工具的「握住」特性存為一個 CFrame 。
代表握住 R02 、握住 R12 和握住 R22 旋轉矩陣的 CFrame 值。
工具的焊接矩陣的位置偏移。
代表握住 R00 、握住 R10 和握住 R20 旋轉矩陣的 CFrame 值。
代表握住 R01 、握住 R11 和握住 R21 旋轉矩陣的 CFrame 值。
手動啟用只屬性控制是否可以無需執行 Tool 即啟用 Tool:Activate() 。
決定是否無處理手把的 Tool 函數。
控制玩家的鼠標停在背包中工具上時顯示的訊息。
顯示在玩家背包中的工具紋理圖示。
為啟用實例傳輸的體驗設置模型的細節程度。
在啟用實例傳輸時,控制模型傳輸行為在 Models 上。
Model 的主要部分,或 nil 如果未明確設設定。
僅用於編輯器的屬性,用於擴展模型周圍的旋轉點。設置此屬性會將比例移至如果 Model/ScaleTo 被呼叫在它上面。
決定哪裡是 不具有設置的 軸心所在的地方。
方法
方法 繼承自 Model將此模型設為指定玩家的持久模型。Model.ModelStreamingMode必須設為 永久每個玩家 以便行為在添加後發生變更。
返回包含模型所有部分的卷的說明。
返回包含所有 BaseParts 在 Model 中最小綁定箱的尺寸,並與 Model.PrimaryPart 一起對齊,如果已設定。
返回此模型對象持有的所有 Player 對象。行為取決於此方法是否從 Script 或 LocalScript 中呼叫。
返回模型的正常比例,預設為新建模型的 1,並且在通過 Model/ScaleTo 縮放時會變更。
將 PrimaryPart 移動到指定位置。如果未指定主要零件,將使用模型的根部分。
使此模型對指定玩家不再持久。Model.ModelStreamingMode必須設為 永久每個玩家 以便在移除後變更行為。
設置模型的縮放因子,調整所有子模塊的大小和位置,使其具有與初始大小和位置相關的縮放因子,當縮放因子為 1 時。
以給定的 Model 偏移值移動 Vector3 ,保留模型的方向。如果另一個 BasePart 或 Terrain 已在新位置存在,那麼 Model 將覆蓋該對物件。
獲得 PVInstance 的軸心。
將 以及所有其子孫 轉換為指定的 位置,使旋轉點現在位於指定的 位置。
活動
當玩家在裝備工具時點擊時,發生火災。
當工具配備和啟用時,玩家釋放點擊時發生火災。
當工具配備時發生火災。
當工具未配備時發生火災。
屬性
CanBeDropped
CanBeDropped 屬性控制玩家是否可以掉落 Tool 。
如果真實,當按下返回空格按鈕時,工具將被傳送到 Workspace 並從玩家的 Backpack 中移除。如果為 false,返回空格鍵時不會發生任何事件,工具將繼續裝備。
Enabled
啟用 屬性關乎是否可以使用Tool。這有用如果你想防止玩家使用工具,但不想從他們的 Backpack 中移除它。
當設為 true 時,玩家可以使用工具。當設為 false 時,工具被禁用,玩家無法使用它;這會使工具無法由 Tool:Activate() 和 Tool:Deactivate() 方法激活或停用,也會阻止 Tool.Activated 和 Tool.Deactivated 事件發射。
範例程式碼
The code sample below creates Tool in the local player's Backpack that increases their JumpPower from 50 to 150 for 5 seconds.
This example uses the tool's Tool.Enabled property as a debounce by setting the property to true when the player jumps and back to false after the 5 second duration.
Unequipping the tool also stops the player from super jumping by changing the JumpPower back to 50.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = Instance.new("Tool")
tool.Name = "SuperJump"
tool.RequiresHandle = false
tool.Parent = player.Backpack
function toolActivated()
humanoid.JumpPower = 150
tool.Enabled = false
task.wait(5)
tool.Enabled = true
humanoid.JumpPower = 50
end
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(function()
humanoid.JumpPower = 50
end)
Grip
Grip儲存工具的"握住"屬性為單個CFrame。這些屬性決定玩家如何握住工具,並包括 GripUp、GripRight、GripForward 和 GripPos。
範例程式碼
The code below insert's a Tool named Stick into the local player's Class.BackPack. When the player activates the tool, the code prints the values of the tool's grip properties.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)
GripForward
指定角色手中工具方向的一個屬性。這代表握住 R02 、 R12 和 R22 旋轉矩陣的值 CFrame 。
控制角色如何握住工具的其他工具屬性包括 Tool.GripUp、Tool.GripRight 和 Tool.GripPos 。這些屬性都儲存在單個 CFrame 中的 Tool.Grip 屬性中。
範例程式碼
The code below insert's a Tool named Stick into the local player's Class.BackPack. When the player activates the tool, the code prints the values of the tool's grip properties.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)
GripPos
此屬性控制工具的焊接矩陣的位置偏移。它是幾個用於定位玩家角色握持工具的屬性之一。
控制角色如何握住工具的其他屬性包括 Tool.GripUp 、 Tool.GripRight 和 Tool.GripForward 。這些屬性都儲存在單個 CFrame 中的 Tool.Grip 屬性中。
範例程式碼
The code below insert's a Tool named Stick into the local player's Class.BackPack. When the player activates the tool, the code prints the values of the tool's grip properties.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)
GripRight
指定角色手中工具方向的一個屬性。這代表握住 R00 、 R10 和 R20 旋轉矩陣的值 CFrame 。
控制角色如何握住工具的其他工具屬性包括 Tool.GripUp、Tool.GripForward 和 Tool.GripPos 。這些屬性都儲存在單個 CFrame 中的 Tool.Grip 屬性中。
範例程式碼
The code below insert's a Tool named Stick into the local player's Class.BackPack. When the player activates the tool, the code prints the values of the tool's grip properties.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)
GripUp
指定角色手中工具方向的一個屬性。這代表握住 R01 、 R11 和 R21 旋轉矩陣的值 CFrame 。
控制角色如何握住工具的其他工具屬性包括 Tool.GripRight、Tool.GripForward 和 Tool.GripPos 。這些屬性都儲存在單個 CFrame 中的 Tool.Grip 屬性中。
範例程式碼
The code below insert's a Tool named Stick into the local player's Class.BackPack. When the player activates the tool, the code prints the values of the tool's grip properties.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local tool = Instance.new("Tool")
tool.Name = "Stick"
tool.Parent = player.Backpack
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Parent = tool
handle.Size = Vector3.new(0.1, 3, 0.1)
handle.Color = Color3.fromRGB(108, 88, 75) -- Brown
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)
ManualActivationOnly
手動啟用僅屬性控制是否可以在腳指令碼中明確地執行 而不需要執行 來啟用 。
當設為真時,工具只會在 Tool.Activated 被呼叫時發射Tool:Activate() 。這也會抑制 ContextActionService:BindActivate() 功能。
當設為 false 時,滑鼠點擊 (當工具配備時) 也會發射 Tool.Activated 。
範例程式碼
The code sample below creates Tool in the local player's Backpack that increases their WalkSpeed from 16 to 30 for 5 seconds.
This example uses the tool's Tool.ManualActivationOnly property as a debounce by setting the property to true when the player begins sprinting and to false when the player stops sprinting. As a result, when the player is sprinting, the tool cannot be re-activated.
Unequipping the tool also stops the player from sprinting by changing the WalkSpeed to 16.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local tool = Instance.new("Tool")
tool.Name = "Sprint"
tool.RequiresHandle = false
tool.Parent = player:WaitForChild("Backpack")
function toolActivated()
humanoid.WalkSpeed = 30
tool.ManualActivationOnly = true
task.wait(5)
tool.ManualActivationOnly = false
humanoid.WalkSpeed = 16
end
tool.Activated:Connect(toolActivated)
tool.Unequipped:Connect(function()
humanoid.WalkSpeed = 16
end)
RequiresHandle
此屬性決定是否無處理手把的 Tool 函數。
工具會有處理器,當包含名為 處理器 的子部件時。通常握把的工具需要玩家裝備它們來保持一個物體以使用它們,例如武器。沒有手柄的工具通常不需要玩家裝備它們來保持任何東西,例如「飛行」或「召喚」工具。
當設為 true 時,工具只能使用處理手把。當設為 false 時,工具即使沒有處理手把也能運作。
ToolTip
工具提示屬性控制會顯示在玩家的 漂浮過 時的訊息。
一般來說,此屬性的值應該描述工具是什麼或其使用。例個體、實例,對於一個鏟子工具,您可以選擇將工具提示設置為:
tool.ToolTip = "Shovel"
or
tool.ToolTip = "Use to dig"
or
tool.ToolTip = "Shovel - Use to dig"
方法
Activate
此功能模擬啟用 Tool 。工具必須裝備才能執行此功能。
返回
範例程式碼
The code below creates a Tool in the local player's Backpack that turns the player invisible when activated and visible when deactivated.
When equipped, the script simulates the tool being activated and turns the player invisible for 3 seconds and then simulates the tool being deactivated. Holding the left mouse button down turns the player invisible for up to 3 seconds, with a cooldown period of 1 second, or until the player releases their left mouse button.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local tool = Instance.new("Tool")
tool.Name = "Invisibility Tool"
tool.RequiresHandle = false
tool.Parent = player.Backpack
local invisible = false
local function toolActivated()
if invisible then
return
end
invisible = true
for _, bodypart in pairs(character:GetChildren()) do
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 1
end
end
task.wait(3)
tool:Deactivate()
task.wait(1)
invisible = false
end
local function toolDeactivated()
if not invisible then
return
end
for _, bodypart in pairs(character:GetChildren()) do
if bodypart.Name ~= "HumanoidRootPart" then
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 0
end
end
end
end
local function toolEquipped()
tool:Activate()
end
tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Deactivated:Connect(toolDeactivated)
tool.Unequipped:Connect(toolDeactivated)
Deactivate
此功能模擬 Tool 的停用。工具必須裝備才能使此功能運作。
返回
範例程式碼
The code below creates a Tool in the local player's Backpack that turns the player invisible when activated and visible when deactivated.
When equipped, the script simulates the tool being activated and turns the player invisible for 3 seconds and then simulates the tool being deactivated. Holding the left mouse button down turns the player invisible for up to 3 seconds, with a cooldown period of 1 second, or until the player releases their left mouse button.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
local tool = Instance.new("Tool")
tool.Name = "Invisibility Tool"
tool.RequiresHandle = false
tool.Parent = player.Backpack
local invisible = false
local function toolActivated()
if invisible then
return
end
invisible = true
for _, bodypart in pairs(character:GetChildren()) do
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 1
end
end
task.wait(3)
tool:Deactivate()
task.wait(1)
invisible = false
end
local function toolDeactivated()
if not invisible then
return
end
for _, bodypart in pairs(character:GetChildren()) do
if bodypart.Name ~= "HumanoidRootPart" then
if bodypart:IsA("MeshPart") or bodypart:IsA("Part") then
bodypart.Transparency = 0
end
end
end
end
local function toolEquipped()
tool:Activate()
end
tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
tool.Deactivated:Connect(toolDeactivated)
tool.Unequipped:Connect(toolDeactivated)
活動
Activated
當玩家按下時,此事件會發生,當 Tool 配備時。如果在點擊期間按下 鍵,它將不會被發射。
此事件通常用於當玩家使用工具時執行行動,例如從火箭發射器武器工具發射火箭。
以下代碼,當放置在 LocalScript 中時,會在本地玩家的 Backpack 創建工具,並在玩家點擊時打印「工具已啟用」。
local Players = game:GetService("Players")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = Players.LocalPlayer.Backpack
function onActivation()
print("Tool activated")
end
tool.Activated:Connect(onActivation)
Deactivated
當 Tool 裝備和啟用時,玩家釋放點擊時,此事件會發生。通常用於當玩家停止使用工具時執行行動。
以下代碼,當放置在 LocalScript 中時,會在本地玩家的 Backpack 創建工具,並在玩家釋放點擊時打印「工具已停用」。
local Players = game:GetService("Players")
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = Players.LocalPlayer.Backpack
function toolDeactivated()
print("Tool deactivated")
end
tool.Deactivated:Connect(toolDeactivated)
Equipped
當玩家裝備 Tool (將它從他們的 Backpack 中移除) 時,此事件會發生。
這個事件的相反,Tool.Unequipped,可以用來確定玩家是否在背包中放置工具時卸下工具。
請注意,此事件在 啟用 》 和沒有處理器存在時不會發射。
參數
玩家的滑鼠。
範例程式碼
The example shown below will print "A tool was equipped" each time the tool is equipped by the player. Please note that the below example assumes that you've already defined what "Tool" is.
local Tool = script.Parent
local function onEquipped(_mouse)
print("The tool was equipped")
end
Tool.Equipped:Connect(onEquipped)
Unequipped
當玩家卸下 (放置在他們的 ) 時,此事件會發生 (將它放入他們的 )。
這個事件的相反,Tool.Equipped,可以用來確定玩家何時裝備工具,將其從背包中取出。
請注意,此事件在 啟用 》 和沒有處理器存在時不會發射。