Tool

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

工具是一個 Humanoid 對象可以裝備的對象。對玩家來說,它們被儲存在一個 Backpack 對象上,該對象與一個 Player 對象相關。在遊戲中,玩家可能會有多個工具,這些工具會在畫面底部出現為圖示。裝備工具將它從背包移至 Player.Character 模型中的 Workspace 模型。預設情況下,工具會被保持在右手中,並擁有一個名為「處理」的手把,其中內部有一個 Part 名為「處理」的物件(雖然如果 Tool.RequiresHandle 關閉,就不需要一個手把)。將提供給重生玩家的工具應該存儲在 StarterPack 中。

在桌面上,按下數字鍵(1、2、3……)會裝備一個工具。裝備的工具可以透過按下「返回鍵」到工作區中掉落。建議您關閉Tool.CanBeDropped以防止掉落工具、死亡、重生並再次掉落工具以複製工具。在手把戲控制器上,LB和RB按鈕會裝備工具。您可以通過設置 Tool.ManualActivationOnly 關閉激活來禁用左鍵點擊(或遊戲手柄上的右觸發器)。這樣做需要您通過某種其他使用者輸入來啟用自己。

工具不是唯一的方法來捕捉使用者輸入。您也可以使用 ContextActionService , UserInputServicePlayer:GetMouse() .如果您需要一個工具具有多個操作,例如在工具配備時按下一個鍵,您應該在 BindActionUnbindAction 事件中使用 ContextActionService 的 EquippedUnequipped,分別使用。使用 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.

Explode Tool Example

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!

Sword Tool Example

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)

概要

屬性

屬性 繼承自 BackpackItem
  • TextureId:ContentId
    平行讀取

    顯示在玩家背包中的工具紋理圖示。

屬性 繼承自 Model屬性 繼承自 PVInstance

方法

方法 繼承自 Model方法 繼承自 PVInstance
  • 平行寫入

    獲得 PVInstance 的軸心。

  • PivotTo(targetCFrame : CFrame):()

    將 以及所有其子孫 轉換為指定的 位置,使旋轉點現在位於指定的 位置。

活動

屬性

CanBeDropped

平行讀取

CanBeDropped 屬性控制玩家是否可以掉落 Tool

如果真實,當按下返回空格按鈕時,工具將被傳送到 Workspace 並從玩家的 Backpack 中移除。如果為 false,返回空格鍵時不會發生任何事件,工具將繼續裝備。

Enabled

平行讀取

啟用 屬性關乎是否可以使用Tool。這有用如果你想防止玩家使用工具,但不想從他們的 Backpack 中移除它。

當設為 true 時,玩家可以使用工具。當設為 false 時,工具被禁用,玩家無法使用它;這會使工具無法由 Tool:Activate()Tool:Deactivate() 方法激活或停用,也會阻止 Tool.ActivatedTool.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.

Superjump Tool

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。這些屬性決定玩家如何握住工具,並包括 GripUpGripRightGripForwardGripPos

範例程式碼

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.

Grip Stick

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

隱藏
未複製
平行讀取

指定角色手中工具方向的一個屬性。這代表握住 R02R12R22 旋轉矩陣的值 CFrame

控制角色如何握住工具的其他工具屬性包括 Tool.GripUpTool.GripRightTool.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.

Grip Stick

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.GripUpTool.GripRightTool.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.

Grip Stick

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

隱藏
未複製
平行讀取

指定角色手中工具方向的一個屬性。這代表握住 R00R10R20 旋轉矩陣的值 CFrame

控制角色如何握住工具的其他工具屬性包括 Tool.GripUpTool.GripForwardTool.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.

Grip Stick

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

隱藏
未複製
平行讀取

指定角色手中工具方向的一個屬性。這代表握住 R01R11R21 旋轉矩陣的值 CFrame

控制角色如何握住工具的其他工具屬性包括 Tool.GripRightTool.GripForwardTool.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.

Grip Stick

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.

Sprint Tool

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.

Invisibility Tool

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.

Invisibility Tool

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,可以用來確定玩家是否在背包中放置工具時卸下工具。

請注意,此事件在 啟用 》 和沒有處理器存在時不會發射。

參數

mouse: Mouse

玩家的滑鼠。


範例程式碼

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.

Print when a Player Equips a Tool

local Tool = script.Parent
local function onEquipped(_mouse)
print("The tool was equipped")
end
Tool.Equipped:Connect(onEquipped)

Unequipped

當玩家卸下 (放置在他們的 ) 時,此事件會發生 (將它放入他們的 )。

這個事件的相反,Tool.Equipped,可以用來確定玩家何時裝備工具,將其從背包中取出。

請注意,此事件在 啟用 》 和沒有處理器存在時不會發射。