Tool

显示已弃用

*此内容使用人工智能(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。这些属性决定玩家如何握住工具,并包括 GripUp , GripRight , GripForwardGripPos .

代码示例

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

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

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

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

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

读取并联

手册激活仅仅属性控制 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.

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 装备时,此事件发生。如果单击期间按下 **** 键,它将不被发射,如果 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 和没有处理器存在时不会触发

参数

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 (将其放入他们的 Backpack ) 时,此事件触发。

这个事件的相反,Tool.Equipped,可以用来确定玩家何时装备工具,将其从背包中取出。

请注意,此事件在启用 Tool.RequiresHandle 和没有处理器存在时不会触发