ツールは、Humanoid オブジェクトが装備できるオブジェクトです。プレイヤーの場合、彼らは Backpack オブジェクトに保存され、Player オブジェクトに親属しています。ゲーム内で、プレイヤーは画面の下部に表示されるアイコンとして複数のツールを持つ可能性があります。ツールを装備すると、バックパックから Player.Character モデルに移動し、Workspace にモデルが移動します。デフォルトでは、ツールは右手に保持され、その中に「ハンドル」という名前のハンドルがあり、内部に Part があります (しかし、Tool.RequiresHandle がオフなら、1つは必要ありません)。再スポーンするプレイヤーに提供されるツールは、StarterPack に保存する必要があります。
デスクトップでは、数字キー (1、2、3など) を押すと、ツールが装備されます。装備されたツールは、バックスペースを押してワークスペースにドロップできます。ツールをドロップしたり、死んだり、再スポーンしたりしてツールを複製することはできないように、Tool.CanBeDropped をオフにすることをお勧めします。ゲームパッドでは、LB と RB ボタンがツールを装備します。有効化を左クリック (またはゲームパッドの右トリガー) で無効にすることができます。Tool.ManualActivationOnly を設定してください。そうするには、別のユーザーの入力を通じて自分を有効にする必要があります。
ツールはユーザーの入力をキャプチャする唯一の方法ではありません。You can also use ContextActionService , UserInputService または Player:GetMouse() も使用できます。ツールに複数のアクションが必要な場合、ツールが装備されている間にキーを押すなど、ツールによるアクションが複数ある場合は、BindAction と UnbindAction の 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)
概要
プロパティ
プレイヤーがツールをドロップできるかどうかを制御します。
ツールを使用できるかどうかに関連します。
ツールの「グリップ」プロパティを 1つの CFrame として保存します。
グリップの R02 , R12 , そして R22 旋回マトリックスの値を表示します: CFrame
ツールの接着マトリックスの位置オフセット。
グリップの R00 , R10 , そして R20 旋回マトリックスの値を表示します: CFrame
グリップの R01 , R11 , そして R21 旋回マトリックスの値を表示します: CFrame
マニュアルアクティベーションのみプロパティは、Tool を実行せずに Tool:Activate() を有効にできるかどうかを制御します。
ハンドルなしで Tool 機能するかどうかを決定します。
プレイヤーのマウスがバックパック内のツールにホバーすると表示されるメッセージを制御します。
プレイヤーのバックパックでツールに表示されるテクスチャアイコン。
インスタンスストリーミングを有効にしたエクスペリエンスのモデルの詳細レベルを設定します。
インスタンスストリーミングが有効になっているときに、モデルストリーミングの動作を制御します Models 。
The primary part of the Model 、または nil が明示的に設定する定されていない場合
モデルの周りにスケールを拡大するために使用される編集者専用のプロパティ。このプロパティを設定すると、スケールが Model/ScaleTo を呼び出したように移動します。
どこに Model が しない を持つピボットが位置するかを決定します。Determines where the pivot of a > which does not have a set Model.PrimaryPart is located.
方法
Model から継承した 方法指定されたプレイヤーのために、このモデルを永続するように設定します。Model.ModelStreamingMode は、追加により変更されるため、 PersistentPerPlayer に設定する必要があります。
モデルのすべての部分を含むボリュームの説明を返します。
返すのは、BaseParts すべてを含む最小のバインディングボックスのサイズ、そして Model に沿って設定されている場合、Model.PrimaryPart と一致します。
このモデルオブジェクトが持続するすべての Player オブジェクトを返します。動作は、このメソッドが Script または LocalScript から呼ばれるかどうかによって異なります。
モデルの正規スケールを返し、新しく作成されたモデルにはデフォルトで 1 が割り当てられ、Model/ScaleTo を介してスケールすると変更されます。
PrimaryPart を指定された位置に移動します。プライマリパーツが指定されていない場合、モデルのルートパーツが使用されます。
指定されたプレイヤーに対して、このモデルを永続しなくする。Model.ModelStreamingMode は、削除により変更されるため、 PersistentPerPlayer に設定する必要があります。
モデルのスケール因子を設定し、すべての子インスタンスのサイズと場所を調整して、スケール因子が 1 のときと比較して、そのスケール因子が初期のサイズと場所に関連しているようにします。
指定された オフセットによってシフトし、モデルの向きを保持します。新しい位置に別の BasePart または Terrain がすでに存在する場合、Model はそのオブジェクトをオーバーライドします。
PVInstance のピボットを取得します。
変換する PVInstance とすべての子孫 PVInstances を通じて、ピボットが現在指定された CFrame に位置します。
イベント
ツールが装備されている間、プレイヤーがクリックすると発火します。
ツールが装備され、有効化されている間、プレイヤーがクリックをリリースすると発火します。
ツールが装備されたときに発火します。
ツールが装備されていないときに発火します。
プロパティ
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
キャラクターの手でツールの向きを指定するプロパティの 1つ。これは、R02、R12、およびR22グリップのCFrame回転マトリックスの値を表します。
キャラクターがツールを保持する方法を制御する他のツールプロパティには、Tool.GripUp、Tool.GripRight、およびTool.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)
GripPos
このプロパティは、ツールの接着マトリックスの位置オフセットを制御します。プレイヤーキャラクターがツールを保持する位置を決定するために使用される複数のプロパティの 1つです。
キャラクターがツールを保持する方法を制御する他のプロパティには、Tool.GripUp、Tool.GripRight、およびTool.GripForwardがあります。これらのプロパティはすべて、 プロパティの単一の中に保存されます。
コードサンプル
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
キャラクターの手でツールの向きを指定するプロパティの 1つ。これは、R00、R10、およびR20グリップのCFrame回転マトリックスの値を表します。
キャラクターがツールを保持する方法を制御する他のツールプロパティには、Tool.GripUp、Tool.GripForward、およびTool.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)
GripUp
キャラクターの手でツールの向きを指定するプロパティの 1つ。これは、R01、R11、およびR21グリップのCFrame回転マトリックスの値を表します。
キャラクターがツールを保持する方法を制御する他のツールプロパティには、Tool.GripRight、Tool.GripForward、およびTool.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)
ManualActivationOnly
ManualActivationOnly プロパティは、スクリプトで明示的に 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 機能するかどうかを決定します。
ツールには、 ハンドル と呼ばれる子部分が含まれているときにハンドルがあります。ハンドル付きのツールは、通常、プレイヤーが装備してオブジェクトを保持し、例えば武器を使用する必要があります。ハンドルのないツールは、通常、「飛ぶ」または「召喚」ツールなど、プレイヤーが装備して使用する必要はありません。
When set to true 、ツールはハンドルのみで機能します。When set to 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 にツールが生成され、作成されたツールを装備している間、プレイヤーがクリックすると「ツールが有効化されました」と印刷されます。
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 とハンドルが存在しないときに火を起こさないことに注意してください。