工具是一個 Backpack 對象可以裝備的工具。對於玩家來說,它們是存放在 Player 對象的父級,1> Class.Player
在桌面上按一個數字鍵(1、2、3...)將裝備一個工具。裝備的工具可以被放在工作區中,按下返回鍵(或右鍵在遊戲手柄上)即可放
工具不是唯一捕捉用戶輸入的方式。您也可以使用 ContextActionService 或 UserInputService 中的 Class.
範例程式碼
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 值的握把旋轉矩陣。
工具的焊接矩陣的位置偏移。
代表 R00 、 R10 和 R20 值的握把旋轉矩陣。
代表 R01 、 R11 和 R21 值的握把旋轉矩陣。
ManualActivationOnly 屬性控制是否能夠啟動 Tool 而不需要執行 Tool:Activate()。
決定是否需要Tool的功能沒有手手把。
控制玩家的滑鼠在他們的背包上移動時顯示的訊息。
玩家背包中的工具所顯示的材質圖示。
設定模型對於啟用時間延遲的體驗的細節程度。
控制 Models 控制器時的模型傳輸行為,當啟用了實例傳輸。
Class.Model 或 nil ,如果未設定。
僅限編輯器的屬性,可以在模型的中心點上調整尺寸。設定此屬性會使尺寸在 Model/ScaleTo 上呼叫。
確定 Model 的中心位置,其中 不 有設置 Model.PrimaryPart 。
方法
方法 繼承自 Model將此模型設定為持續為指定玩家。 Model.ModelStreamingMode 必須設置為 PersistentPerPlayer 才能因為添加而變更行為。
返回包含模型所有部分的音量的說明。
返回 BaseParts 的尺寸,Model 與 Model.PrimaryPart 如果設定。
返回此模型對象的所有 Player 對象。 行為因 Script 或 LocalScript 而來自。
返回模型的標準尺寸,其預設為 1 對於新建的模型,並且會在通過 Model/ScaleTo 變更時更改。
將 PrimaryPart 移動到指定位置。如果沒有指定主要零件,則會使用模型的根部分。
此模型不再對指定玩家持續,必須設置為 Model.ModelStreamingMode 才能變更行為,作為結果的一部分。
設定模型的比例因素,以調整所有子孫實例的大小和位置,使其在比例因素變更 1 時,對眾生實例的大小和位置進行相對的比例調整。
將 Model 由所提供的 Vector3 偏移值,保留模型的方向。如果另一個 BasePart 或 1> Class.Terrain1> 已在新位置,則 4> Class.Model4> 將覆蓋所述對物件。
取得 PVInstance 的中心點。
以下是 PVInstance 和所有其子 PVInstances 的描述,以便 pivot 現在位於指定的 CFrame 。
活動
裝備工具時,玩家點擊時發射。
發射工具裝備和啟用時玩家放開按鈕的瞬間。
裝備工具時會發射。
工具未裝備時發射。
屬性
CanBeDropped
CanBeDropped 屬性控制玩家是否能夠放下 Tool 。
如果是,當按下返回鍵時,工具將會親綱在 Workspace 並從玩家的 Backpack 中移除。如果是,在按下返回鍵時,工具將會裝備。
Enabled
啟用屬性 與否 關閉Tool 的使用。這有助於防止玩家使用工具,但不需要將其從他們的Backpack 移除。
當設為 true 時,玩家可以使用工具。當設為 false 時,工具將無法使用,因為它無法啟動或關閉 Tool:Activate() 和 2>Class.Tool:Deactivate2> 方法。這
範例程式碼
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 。這些屬性位置如何玩家持有工具並包括 1>Class
範例程式碼
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 值的握把旋轉矩陣。
控制角色持有工具的方式的其他工具屬性包括 Tool.GripUp , Tool.GripRight 和 Tool.GripPos . 所有這些屬性都存在在 1> Datatype.CFrame1> 中的單個 4> Class.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 。這些屬性都存放在單個 1> Datatype.CFrame1> 在 4> Class.Tool.Grip4> ��
範例程式碼
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 值的握把旋轉矩陣。
控制角色持有工具的方式的其他工具屬性包括 Tool.GripUp , Tool.GripForward 和 Tool.GripPos . 所有這些屬性都存在在 1> Datatype.CFrame1> 中的單個 4> Class.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 值的握把旋轉矩陣。
控制角色持有工具的方式的其他工具屬性包括 Tool.GripRight , Tool.GripForward 和 Tool.GripPos . 所有這些屬性都存在在 1> Datatype.CFrame1> 中的單個 4> Class.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
ManualActivationOnly 屬性控制是否需要在 Tool 可以啟動無需直接執行 Tool:Activate() 在指令碼中。
設為"真"時, 工具只會在 Tool.Activated 被呼叫時發射。這也會消除 Tool:Activate() 的功能。
設為 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
此屬性決定 whether a Tool 功能無處理手把。
當工具包含名為 Handle 的子零件時,工具會提供一個手柄。工具與手柄一般需要玩家裝備它們才能使用,例如武器。沒有手柄的工具通常不需要裝備玩家才能使用,例如 fly 或 summon 工具。
當設為 true 時,工具只能與手把子使用。當設為 false 時,工具可以使用無手把子。
方法
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 正在裝備時。它不會發生在 Ctrl 鍵被按住期間,如果按下 Ctrl 鍵。
此事件通常用於執行玩家使用工具時的行動,例如從火箭發射器武器工具發射火箭。
下面的代碼,當放置在 LocalScript 中,會在本玩家的 Backpack 中創建一個工具,並且在玩家按一下時,工具會打印為 "已啟用"。
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = game.Players.LocalPlayer.Backpack
function onActivation()
print("Tool activated")
end
tool.Activated:Connect(onActivation)
Deactivated
這個事件會在玩家釋放其按鈕時發生,而 Tool 裝備並啟用時。它通常用於在玩家停止使用工具時執行一個操作。
下面的代碼,當放置在 LocalScript 中,會在本玩家的 Backpack 中創建一個工具,並且在工具裝備和啟用時,會打印「工具已禁用」。
local tool = Instance.new("Tool")
tool.RequiresHandle = false
tool.Parent = game.Players.LocalPlayer.Backpack
function toolDeactivated()
print("Tool deactivated")
end
tool.Deactivated:Connect(toolDeactivated)
Equipped
這個事件會發生,當玩家裝備 Tool (從他們的 Backpack 中取出)。
這個事件的相反,Tool.Unequipped,可以用來決定玩家是否在背包中放置工具,以便決定他們是否要卸下工具。
注意,當 Class.Tool.RequiresHandle 啟用時,此事件不會發生 Tool.RequiresHandle 發生火災,但沒有處理器。
參數
玩家的滑鼠。
範例程式碼
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 (將它放進他們的 Backpack 裡)。
這個事件的相反, Tool.Equipped,可以用來決定玩家是否裝備工具,從背包中拿出工具。
注意,當 Class.Tool.RequiresHandle 啟用時,此事件不會發生 Tool.RequiresHandle 發生火災,但沒有處理器。