工具是一个对象,一个 Class.Humanoid 对象可以装备。 对玩家来说,它们存储在一个 Class.Backpack 对象上,其父对象是 0> Class.Player
在桌面上,按一个数字键(1、2、3...)将装备一个工具。装备的工具可以通过按“返回键”(或“右键”在游戏手柄上)放入工作区。 建议您关闭 Class
工具不是唯一的捕获用户输入的方法。您也可以使用 ContextActionService 和 UserInputService 事件来将这些
代码示例
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握把1> Datatype.CFrame1>旋转矩阵的价值。
工具的焊接矩阵的位置偏移。
代表R00、R10和R20握把1> Datatype.CFrame1>旋转矩阵的价值。
代表R01、R11和R21握把旋转矩阵的1> Datatype.CFrame1>值。
手动激活只有属性控制是否可以在不执行 Tool 的情况下激活。
确定是否需要一个手柄才能使用 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 必须设置为 PersistentPerPlayer 才能因为移除而改变行为。
设置模型的缩放因素,调整所有子集实例的尺寸和位置,使其在缩放因素为 1 时,相对于其初始大小和位置拥有该缩放因素。
使用 Model 的给定 Offset,将模型的方向保偏移值,如果另一个 Vector3 或 BasePart 已经在新位置,那么 1> Class.Model1> 将覆盖该对象。
获得 PVInstance 的枢轴。
形成 PVInstance 与所有的后代 PVInstances ,使 pivot 现在位于指定的 CFrame 。
活动
当玩家在装备工具时点击时,火焰会发生。
在工具装备并激活时,玩家释放其点击时发生错误。
装备工具时触发。
工具未装备时触发。
属性
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.
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.
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 。 所有这些属性都存储在 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.
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 。 所有这些属性都存储在 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.
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 。 所有这些属性都存储在 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.
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 。 所有这些属性都存储在 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.
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.
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 时,工具可以使用无需手柄。
方法
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 中时,下面的代验证码创建一个工具在本地玩家的 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 发生,因为没有手柄。
参数
玩家的鼠标。
代码示例
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 发生,因为没有手柄。