工具是 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
手册激活仅仅属性控制 whether Tool 可以无需执行 Tool:Activate() 即可激活。
决定是否一个 Tool 函数没有手柄。
控制玩家的鼠标悬停在背包中工具上时显示的消息。
显示在玩家背包中的工具图标的纹理。
将模型上的细节级别设置为启用实例流式传输的体验。
在启用实例流时控制模型传输行为在 Models 上,当实例流启用时。
Model 的主部分,或 nil 如果未明确设置。
仅用于编辑器的属性用于在模型周围缩放。设置此属性会将缩放移至如果 Model/ScaleTo 被调用在它上。
决定哪里是 Model 做 不 有设置 Model.PrimaryPart 的旋转点。
方法
继承自Model的方法将此模型设置为对指定玩家持续有效。Model.ModelStreamingMode必须设置为 PersistentPerPlayer ,以便通过添加而改变行为。
返回包含模型所有部分的卷的描述。
返回包含所有 BaseParts 在 Model 中的最小边界盒的大小,对齐 Model.PrimaryPart 如果已设置。
返回这个模型对象持有的所有 Player 对象。行为取决于这个方法是从 Script 或 LocalScript 中调用。
返回模型的标准缩放比例,默认为新创建的模型为 1,通过 Model/ScaleTo 缩放时将发生变化。
将 PrimaryPart 移至指定位置。如果未指定主部件,将使用模型的根部件。
使该模型不再对指定玩家持续存在。Model.ModelStreamingMode必须设置为 PersistentPerPlayer ,以便通过移除而更改行为。
设置模型的缩放因子,调整所有子模块的大小和位置,使其具有与初始大小和位置相关的缩放因子,当缩放因子为 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
手册激活仅仅属性控制 whether Tool 可以在脚本中无需执行 Tool:Activate() 来启用。
当设置为真时,工具只会在 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 装备时,此事件发生。如果单击期间按下 **** 键,它将不被发射,如果 Ctrl 键被按下期间。
此事件通常用于当玩家使用工具时执行行动,例如从火箭发射器武器工具发射火箭。
以下代验证码,放置在 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,可以用来确定玩家何时卸下工具,将其放入背包。
请注意,此事件在启用 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,可以用来确定玩家何时装备工具,将其从背包中取出。
请注意,此事件在启用 Tool.RequiresHandle 和没有处理器存在时不会触发 火 。