Tool

Tampilkan yang Tidak Digunakan Lagi

*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.

Alat adalah objek yang dapat dilengkapi oleh objek Humanoid .Untuk pemain, mereka disimpan dalam objek Backpack orangtua ke objek Player .Di dalam game, pemain mungkin memiliki beberapa alat yang muncul sebagai ikon di bagian bawah layar.Melengkapi alat memindahkannya dari Backpack dan ke dalam model di .Secara default, alat dipegang di tangan kanan dan memiliki pegangan di dalamnya, yang merupakan Part bernama "Handle" di dalam (meskipun satu tidak diperlukan jika Tool.RequiresHandle mati).Alat yang harus disediakan untuk (re)memunculkan pemain harus disimpan di StarterPack .

Di desktop, menekan tombol angka (1, 2, 3...) akan melengkapi alat.Alat yang dilengkapi dapat dilemparkan ke Workspace dengan menekan Backspace.Disarankan agar Anda mematikan Tool.CanBeDropped off sehingga tidak mungkin menjatuhkan alat, mati, respawn dan jatuh lagi untuk menyalin alat.Di gamepad, tombol LB dan RB akan melengkapi alat.Anda dapat menonaktifkan aktivasi melalui klik kiri (atau pemicu kanan di gamepad) dengan mengatur Tool.ManualActivationOnly pada.Melakukan hal itu memerlukan agar Anda memanggil Aktifkan diri sendiri melalui beberapa jenis input pengguna lain.

Alat bukan satu-satunya cara untuk menangkap input pengguna.Anda juga dapat menggunakan ContextActionService , UserInputService atau Player:GetMouse() .Jika Anda membutuhkan Alat untuk memiliki banyak tindakan, seperti menekan tombol saat Alat dilengkapi, Anda harus menggunakan ContextActionService's BindAction dan UnbindAction dalam peristiwa Equipped dan Unequipped, masing-masing.Gunakan LocalScript mengirimkan tindakan ini ke server melalui RemoteFunction di dalam Alat.

Contoh Kode

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)

Rangkuman

Properti

Properti diwarisi dari BackpackItem
  • TextureId:ContentId
    Baca Paralel

    Ikon tekstur yang ditampilkan untuk alat di ransel pemain.

Properti diwarisi dari Model
  • Keamanan Plugin
    Baca Paralel

    Mengatur tingkat rincian pada model untuk pengalaman dengan streaming instansi diaktifkan.

  • Mengontrol perilaku streaming model di Models ketika streaming instansi diaktifkan.

  • Baca Paralel

    Bagian utama dari Model , atau nil jika tidak secara eksplisit atur.

  • Tidak Direplikasi
    Tidak Dapat Ditulis Skripnya
    Baca Paralel

    Properti hanya editor yang digunakan untuk memperluas model di sekitar titik pivotnya. Mengatur properti ini akan memindahkan skala seolah-olah Model/ScaleTo dipanggil di atasnya.

  • Tidak Direplikasi
    Baca Paralel

    Menentukan di mana titik pivot dari yang tidak memiliki set terletak.

Properti diwarisi dari PVInstance

Metode

Metode diwarisi dari Model
  • AddPersistentPlayer(playerInstance : Player):()

    Atur model ini menjadi permanen untuk pemain yang ditentukan. Model.ModelStreamingMode harus diatur ke PersistentPerPlayer agar perilaku berubah sebagai hasil dari tambahan.

  • Kembalikan deskripsi volume yang berisi semua bagian dari Model.

  • Kembalikan ukuran kotak batas terkecil yang berisi semua BaseParts di dalam Model, sesuai dengan Model.PrimaryPart jika diatur.

  • Kembalikan semua objek Player yang persisten untuk objek model ini.Perilaku bervariasi tergantung pada apakah metode ini dipanggil dari Script atau LocalScript.

  • Kembalikan skala kanonik model, yang defaultnya adalah 1 untuk model yang baru dibuat dan akan berubah saat skalanya diubah melalui Model/ScaleTo .

  • MoveTo(position : Vector3):()

    Pindahkan PrimaryPart ke posisi yang diberikan. Jika bagian utama belum ditentukan, bagian akar model akan digunakan.

  • RemovePersistentPlayer(playerInstance : Player):()

    Membuat model ini tidak lagi permanen untuk pemain yang ditentukan. Model.ModelStreamingMode harus diatur ke PersistentPerPlayer agar perilaku diubah sebagai hasil dari penghapusan.

  • ScaleTo(newScaleFactor : number):()

    Mengatur faktor skala model, menyesuaikan ukuran dan lokasi semua Instans anak sehingga mereka memiliki faktor skala relatif terhadap ukuran dan lokasi awal mereka saat faktor skala adalah 1.

  • TranslateBy(delta : Vector3):()

    Bergeser ke Model dengan offset yang diberikan Vector3 , menyimpan orientasi model.Jika lain BasePart atau Terrain sudah ada di posisi baru maka Model akan tumpang tindih dengan objek yang dikatakan.

Metode diwarisi dari PVInstance

Acara

Properti

CanBeDropped

Baca Paralel

Properti CanBeDropped mengontrol apakah pemain dapat menjatuhkan Tool.

Jika benar, saat tombol backspace ditekan, alat akan diberikan ke Workspace dan dihapus dari pemain Backpack .Jika salah, tidak ada yang terjadi saat backspace ditekan, dan alat akan tetap dilengkapi.

Enabled

Baca Paralel

Properti Diaktifkan terkait dengan apakah atau tidak Tool dapat digunakan.Ini berguna jika Anda ingin mencegah pemain menggunakan alat, tetapi tidak ingin menghapusnya dari Backpack mereka.

Ketika diatur ke true, pemain dapat menggunakan alat.Ketika diatur ke false , alat dinonaktifkan dan pemain tidak dapat menggunakannya; ini mencegah alat dari diaktifkan atau dimatikan oleh metode Tool:Activate() dan Tool:Deactivate() , dan mencegah peristiwa Tool.Activated dan Tool.Deactivated terjadi.

Contoh Kode

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

Baca Paralel

Properti Grip menyimpan properti "grip" alat sebagai satu CFrame .Properti ini menentukan posisi pemain memegang alat dan termasuk GripUp , GripRight , GripForward , dan GripPos .

Contoh Kode

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

Tersembunyi
Tidak Direplikasi
Baca Paralel

Salah satu properti yang menentukan orientasi alat di tangan karakter.Ini mewakili R02 , R12 , dan R22 nilai gesekan CFrame matriks rotasi.

Properti alat lain yang mengontrol bagaimana karakter memegang alat termasuk Tool.GripUp , Tool.GripRight , dan Tool.GripPos .Semua properti ini disimpan dalam satu CFrame properti Tool.Grip.

Contoh Kode

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

Tersembunyi
Tidak Direplikasi
Baca Paralel

Properti ini mengontrol offset posisi matriks las alat.Ini adalah salah satu dari beberapa properti yang digunakan untuk menentukan posisi bagaimana karakter pemain memegang alat.

Properti lain yang mengontrol bagaimana seorang karakter memegang alat termasuk Tool.GripUp , Tool.GripRight , dan Tool.GripForward .Semua properti ini disimpan dalam satu CFrame properti Tool.Grip.

Contoh Kode

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

Tersembunyi
Tidak Direplikasi
Baca Paralel

Salah satu properti yang menentukan orientasi alat di tangan karakter.Ini mewakili R00 , R10 , dan R20 nilai gesekan CFrame matriks rotasi.

Properti alat lain yang mengontrol bagaimana karakter memegang alat termasuk Tool.GripUp , Tool.GripForward , dan Tool.GripPos .Semua properti ini disimpan dalam satu CFrame properti Tool.Grip.

Contoh Kode

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

Tersembunyi
Tidak Direplikasi
Baca Paralel

Salah satu properti yang menentukan orientasi alat di tangan karakter.Ini mewakili R01 , R11 , dan R21 nilai gesekan CFrame matriks rotasi.

Properti alat lain yang mengontrol bagaimana karakter memegang alat termasuk Tool.GripRight , Tool.GripForward , dan Tool.GripPos .Semua properti ini disimpan dalam satu CFrame properti Tool.Grip.

Contoh Kode

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

Baca Paralel

Properti Aktivasi Manual Hanya mengontrol apakah Tool dapat diaktifkan tanpa mengeksekusi Tool:Activate() secara eksplisit dalam skrip.

Ketika diatur ke benar, alat hanya akan menembak Tool.Activated ketika Tool:Activate() dipanggil. Ini juga menekan fungsi ContextActionService:BindActivate().

Ketika diatur ke false, klik mouse (ketika alat dilengkapi) juga akan menembakkan Tool.Activated .

Contoh Kode

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

Baca Paralel

Properti ini menentukan apakah fungsi Tool tanpa pegangan.

Alat memiliki pegangan ketika berisi bagian anak bernama Pegangan .Alat dengan pegangan biasanya memerlukan pemain untuk melengkapinya untuk memegang objek untuk menggunakannya, misalnya senjata.Alat tanpa pegangan biasanya tidak memerlukan pemain untuk memakainya untuk memegang sesuatu untuk menggunakannya, misalnya alat "terbang" atau "memanggil"

Ketika diatur ke true , alat hanya akan berfungsi dengan pegangan. Ketika diatur ke false , alat akan berfungsi bahkan tanpa pegangan.

ToolTip

Baca Paralel

Properti ToolTip mengontrol pesan yang akan ditampilkan saat pemain Mouse melewati Tool di dalam Backpack mereka.

Secara umum, nilai properti ini harus menjelaskan apa alat itu atau penggunaannya.Sebagai kejadian, untuk alat sekop, Anda dapat memilih untuk mengatur ToolTip ke:


tool.ToolTip = "Shovel"

or


tool.ToolTip = "Use to dig"

or


tool.ToolTip = "Shovel - Use to dig"

Metode

Activate

()

Fungsi ini meniru aktivasi Tool . Alat harus dilengkapi agar fungsi ini berfungsi.


Memberikan nilai

()

Contoh Kode

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

()

Fungsi ini menyimulasikan deaktivasi Tool . Alat harus dilengkapi agar fungsi ini berfungsi.


Memberikan nilai

()

Contoh Kode

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)

Acara

Activated

Acara ini terjadi ketika pemain mengklik saat Tool dilengkapi.Ini tidak ditembak jika kunci Ctrl ditekan selama klik.

Acara ini biasanya digunakan untuk melakukan tindakan ketika pemain menggunakan alat, misalnya untuk meluncurkan roket dari alat peluncur roket.

kodedi bawah ini, saat ditempatkan di LocalScript , membuat alat di Backpack pemain lokal dan mencetak "Alat diaktifkan" saat pemain mengklik saat alat yang dibuat dilengkapi.


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

Acara ini terjadi ketika pemain merilis klik mereka saat Tool dilengkapi dan diaktifkan.Biasanya digunakan untuk melakukan tindakan ketika pemain berhenti menggunakan alat.

kodedi bawah ini, saat ditempatkan di LocalScript , membuat alat di pemutar lokal Backpack dan mencetak "Alat dinonaktifkan" saat pemain merilis klik mereka saat alat dilengkapi dan diaktifkan.


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

Peristiwa ini terjadi ketika pemain memperlengkapi Tool (menghapuskannya dari Backpack ).

Kebalikan dari peristiwa ini, Tool.Unequipped , dapat digunakan untuk menentukan kapan pemain melepaskan alat dengan menempatkannya di ransel mereka.

Perhatikan bahwa acara ini tidak tidak terbakar saat Tool.RequiresHandle diaktifkan dan tidak ada penangan hadir.

Parameter

mouse: Mouse

mousepemain.


Contoh Kode

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

Peristiwa ini terjadi ketika pemain melepaskan Tool (menempatkannya di Backpack mereka).

Kebalikan dari peristiwa ini, Tool.Equipped , dapat digunakan untuk menentukan kapan pemain memperlengkapi alat dengan mengeluarkannya dari ransel mereka.

Perhatikan bahwa acara ini tidak tidak terbakar saat Tool.RequiresHandle diaktifkan dan tidak ada penangan hadir.