Tool
*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.
Un Tool è un oggetto che un oggetto Humanoid può equipaggiare. Per i giocatori, vengono memorizzati in un oggetto Backpack associato a un Player oggetto. In esperienza, i giocatori possono avere più strumenti che appaiono come icone nella parte inferiore dello schermo. Equipaggiare uno strumento lo sposta dal Backpack e in un modello Player.Character nel Workspace. Per impostazione predefinita, gli strumenti sono tenuti nella mano destra e hanno un manico in essi, che è un BasePart chiamato Handle all'interno (anche se uno non è richiesto se Tool.RequiresHandle è false). Gli strumenti che devono essere forniti ai giocatori (ri)spawnati dovrebbero essere memorizzati nel StarterPack.
Campioni di codice
Questo codice è destinato ad essere inserito in uno Script all'interno di uno Strumento. Permette a un giocatore di generare esplosioni equipaggiando lo strumento e cliccando a terra. Per farlo, definisce una funzione, explode, che crea un'esplosione non letale in un punto specifico. Poi, definisce una funzione, onActivated, che viene eseguita quando lo strumento viene attivato. Infine, collega l'evento Activated dello strumento alla funzione onActivated.
Per provare questo codice, prova a creare uno Strumento e a mettere un Parte al suo interno. Nomina la Parte "Handle". Metti uno Script all'interno dello Strumento successivamente e incolla il codice al suo interno. Infine, metti lo Strumento in StarterPack.
local tool = script.Parent
local function explode(point)
local e = Instance.new("Explosion")
e.DestroyJointRadiusPercent = 0 -- Rendi l'esplosione non letale
e.Position = point
e.Parent = workspace
end
local function onActivated()
-- Ottieni il Humanoid che ha attivato lo strumento
local human = tool.Parent.Humanoid
-- Chiama explode con il punto corrente che il Humanoid sta mirando
explode(human.TargetPoint)
end
tool.Activated:Connect(onActivated)Questo esempio di codice è per un oggetto Strumento con una Parte chiamata Manico. Rileva quando gli Umani diversi dal portatore attuale colpiscono il manico e infligge alcuni danni a loro. Inoltre, quando lo Strumento viene attivato, attiva un'animazione per un colpo nei script di animazione predefiniti del personaggio. Prova questo script creando un oggetto Strumento nel StarterPack. Metti una Parte all'interno e chiamala Manico. Incolla questo codice in uno Script all'interno dello Strumento, poi prova a colpire un altro Umano!
local tool = script.Parent
local function onTouch(partOther)
-- Prima, prova a vedere se la parte che abbiamo toccato faceva parte di un Umano
local humanOther = partOther.Parent:FindFirstChild("Humanoid")
-- Ignora i tocchi da parte di non-umani
if not humanOther then
return
end
-- Ignora i tocchi da parte dell'Umano che porta la spada
if humanOther.Parent == tool.Parent then
return
end
humanOther:TakeDamage(5)
end
-- Attiva un'animazione di colpo
local function slash()
-- Gli script di personaggio predefiniti ascolteranno un valore StringValue chiamato "toolanim"
local value = Instance.new("StringValue")
value.Name = "toolanim"
value.Value = "Colpo" -- prova anche: Lunge
value.Parent = tool
end
tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)Sommario
Proprietà
Controlla se il giocatore può lasciare cadere lo strumento.
Riguarda se lo strumento può essere utilizzato o meno.
Memorizza le proprietà di "grip" dello strumento come un CFrame.
Rappresenta i valori R02, R12 e R22 della matrice di rotazione del grip CFrame.
L'offset posizionale della matrice di saldatura dello strumento.
Rappresenta i valori R00, R10 e R20 della matrice di rotazione del grip CFrame.
Rappresenta i valori R01, R11 e R21 della matrice di rotazione del grip CFrame.
Controlla se il Tool può essere attivato senza eseguire Tool:Activate().
Determina se un Tool funziona senza un manico.
Controlla il messaggio visualizzato quando il mouse del giocatore passa sopra lo strumento nel suo zaino.
Proprietà
L'icona della texture che viene visualizzata per uno strumento nello zaino del giocatore. Supporta solo URI di asset.
L'icona della texture che viene visualizzata per uno strumento nello zaino del giocatore.
Proprietà
Imposta il livello di dettaglio sul modello per esperienze con streaming dell'istanza abilitato.
Controlla il comportamento di streaming del modello su Models quando lo streaming dell'istanza è abilitato.
La parte primaria del Model, o nil se non impostata esplicitamente.
Proprietà riservata all'editor utilizzata per scalare il modello attorno al suo punto di pivot. Impostare questa proprietà sposterà la scala come se Model:ScaleTo() fosse stato chiamato su di essa.
Determines where the pivot of a Model which does not have a set Model.PrimaryPart is located.
Proprietà
Metodi
Metodi provenienti da ModelMetodi
Sets this model to be persistent for the specified player. ModelStreamingMode must be set to PersistentPerPlayer for behavior to be changed as a result of addition.
Returns a description of a volume that contains all parts of a Model.
Returns the size of the smallest bounding box that contains all of the BaseParts in the Model, aligned with the Model.PrimaryPart if it is set.
Returns all the Player objects that this model object is persistent for. Behavior varies based on whether this method is called from a Script or a LocalScript.
Restituisce la scala canonica del modello, che di default è 1 per i modelli nuovamente creati e cambierà man mano che viene scalato tramite Model:ScaleTo().
Moves the PrimaryPart to the given position. If a primary part has not been specified, the root part of the model will be used.
Makes this model no longer persistent for the specified player. ModelStreamingMode must be set to PersistentPerPlayer for behavior to be changed as a result of removal.
Sets the scale factor of the model, adjusting the sizing and location of all descendant Instances such that they have that scale factor relative to their initial sizes and locations when scale factor was 1.
Shifts a Model by the given Vector3 offset, preserving the model's orientation. If another BasePart or Terrain already exists at the new position then the Model will overlap said object.
Metodi
Ottiene il pivot di una PVInstance.
Trasforma il PVInstance insieme a tutti i suoi discendenti PVInstances in modo che il pivot si trovi ora nella specificata CFrame.
Eventi
Si attiva quando il giocatore clicca mentre lo strumento è equipaggiato.
Si attiva quando il giocatore rilascia il clic mentre lo strumento è equipaggiato e attivato.
Si attiva quando lo strumento è equipaggiato.
Si attiva quando lo strumento viene disequipaggiato.
Proprietà
CanBeDropped
Enabled
Campioni di codice
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
Campioni di codice
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) -- Marrone
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)GripForward
Campioni di codice
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) -- Marrone
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)GripPos
Campioni di codice
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) -- Marrone
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)GripRight
Campioni di codice
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) -- Marrone
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)GripUp
Campioni di codice
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) -- Marrone
tool.Activated:Connect(function()
print(tool.Grip)
print(tool.GripUp)
print(tool.GripRight)
print(tool.GripForward)
print(tool.GripPos)
end)ManualActivationOnly
Campioni di codice
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
ToolTip
Metodi
Activate
Restituzioni
Campioni di codice
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
Restituzioni
Campioni di codice
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)Eventi
Activated
Deactivated
Equipped
Parametri
Campioni di codice
local Tool = script.Parent
local function onEquipped(_mouse)
print("Lo strumento è stato equipaggiato")
end
Tool.Equipped:Connect(onEquipped)