Tool

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

工具是一个对象,一个 Class.Humanoid 对象可以装备。 对玩家来说,它们存储在一个 Class.Backpack 对象上,其父对象是 0> Class.Player

在桌面上,按一个数字键(1、2、3...)将装备一个工具。装备的工具可以通过按“返回键”(或“右键”在游戏手柄上)放入工作区。 建议您关闭 Class

工具不是唯一的捕获用户输入的方法。您也可以使用 ContextActionServiceUserInputService 事件来将这些

代码示例

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)

概要

属性

  • 读取并联

    控制玩家是否能够丢弃工具。

  • 读取并联

    关系工具是否可用。

  • 读取并联

    将“握把”属性存储为一个 CFrame

  • 隐藏
    未复制
    读取并联

    代表R02R12R22握把1> Datatype.CFrame1>旋转矩阵的价值。

  • 隐藏
    未复制
    读取并联

    工具的焊接矩阵的位置偏移。

  • 隐藏
    未复制
    读取并联

    代表R00R10R20握把1> Datatype.CFrame1>旋转矩阵的价值。

  • 隐藏
    未复制
    读取并联

    代表R01R11R21握把旋转矩阵的1> Datatype.CFrame1>值。

  • 手动激活只有属性控制是否可以在不执行 Tool 的情况下激活。

  • 读取并联

    确定是否需要一个手柄才能使用 Tool 函数。

  • 读取并联

    控制玩家鼠标悬停在工具背包上时显示的消息。

继承自BackpackItem属性
  • TextureId:ContentId
    读取并联

    玩家背包中的工具的材质图标。

继承自Model属性继承自PVInstance属性

方法

继承自Model方法继承自PVInstance方法

活动

属性

CanBeDropped

读取并联

CanBeDropped 属性控制玩家是否能够丢弃 Tool

如果是真的,当按下返回键时,工具将被父级到 Workspace 并从玩家的 Backpack 中移除。 如果是假的,在按下返回键时,工具将保持装备。

Enabled

读取并联

启用属性与否使用Tool 有关。如果您想要防止玩家使用工具,但又不想将其从他们的Backpack 中移除,则可以使用此属性。

当设置为 true 时,玩家可以使用工具。当设置为 false 时,工具已禁用,玩家无法使用它;这防止工具被激活或关闭通过 Tool:Activate() 和 2>Class.Tool:Deactivate</

代码示例

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 属性存储工具的“grip”属性为单个 CFrame 。这些属性位置如何玩家持有工具并包括 GripUp , 1>Class.Tool.GripRight|G

代码示例

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 值的握把旋转矩阵。

其他控制角色持有工具的属性包括 Tool.GripUpTool.GripRightTool.GripPos 。 所有这些属性都存储在 2>Class.Tool.Grip2> 属性中。

代码示例

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.GripRightTool.GripForward 。 所有这些属性都存储在 2>Class.Tool.Grip2> 属性中。

代码示例

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 值的握把旋转矩阵。

其他控制角色持有工具的属性包括 Tool.GripUpTool.GripForwardTool.GripPos 。 所有这些属性都存储在 2>Class.Tool.Grip2> 属性中。

代码示例

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 值的握把旋转矩阵。

其他控制角色持有工具的属性包括 Tool.GripRightTool.GripForwardTool.GripPos 。 所有这些属性都存储在 2>Class.Tool.Grip2> 属性中。

代码示例

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

读取并联

手动激活Only 属性控制是否可以在不需要明确执行 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.

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 的函数是否具有手柄。

一个工具包含一个名为 Handle 的子部分时,其手柄通常由玩家装备,以便使用它。例如,工具有手柄的话,通常需要玩家装备它们来持有一个对象,例如武器。没有手柄的工具通常不需要玩家装备它们来持有任何东西以使用它们,例如“飞行”或“召唤”工具。

设置为 true 时,工具只能使用手柄。设置为 false 时,工具可以使用无需手柄。

ToolTip

读取并联

工具提示属性控制显示玩家的 Mouse 将鼠标悬停在 Tool 上,当玩家的 Backpack 处于其 2>Class.Tool2> 上时显示的消息。

一般来说,这个属性的值应该描述工具是什么或其使用。例实例,对于铲子工具,您可以选择将ToolTip设置为:


tool.ToolTip = "Shovel"


tool.ToolTip = "Use to dig"


tool.ToolTip = "Shovel - Use to dig"

方法

Activate

void

此函数模拟激活 Tool 。工具必须装备才能使用此功能。


返回

void

代码示例

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

void

此函数模拟 Tool 的关闭。工具必须装备才能使该功能工作。


返回

void

代码示例

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 中时,下面的代验证码创建一个工具在本地玩家的 Class.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 中时,它会创建一个工具在本地玩家的 Class.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 发生,因为没有手柄。

参数

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,可以用来确定玩家是否装备工具,从背包中拿出工具。

注意,当 Class.Tool.RequiresHandle 启用时,此事件不会发生 Tool.RequiresHandle 发生,因为没有手柄。