Prompt
Answer
Roblox Fire Hydrant Tool Script
This document provides a Lua script for a Roblox tool that implements a fire hydrant mechanics. The tool includes various features like audio playback on equip, collision handling, custom animations, and damage implementation upon throwing the hydrant.
Function Overview
Features
- Plays audio when equipped
- Ignores collision with the player who throws it
- Plays a custom animation and sound when used
- Thrown to a click position after the animation finishes
- Ragdolls other players hit and deals 50 damage
- Implements a cooldown of 3.5 seconds for usage
- The thrown hydrant disappears after 3.5 seconds
Required Imports
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
Tool Script Implementation
-- Fire Hydrant Tool Script
-- Function to initialize the throw action of the hydrant
local function throwHydrant(player, hydrant, animation, audio)
-- Play the animation
local animTrack = player.Character:FindFirstChildOfClass("Animator"):LoadAnimation(animation)
animTrack:Play()
-- Play the audio when using the hydrant
audio:Play()
-- Wait until animation is completed
wait(animTrack.Length)
-- Get the mouse click position
local mouse = player:GetMouse()
local targetPosition = mouse.Hit.p
-- Create a thrown hydrant instance
local thrownHydrant = hydrant:Clone()
thrownHydrant.Position = player.Character.HumanoidRootPart.Position
thrownHydrant.Parent = workspace
-- Ignore collision with the player
thrownHydrant.CanCollide = false
-- Tween to the target position
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear)
local tween = TweenService:Create(thrownHydrant, tweenInfo, {Position = targetPosition})
-- Fire ragdolling mechanism
tween.Completed:Connect(function()
for _, v in ipairs(workspace:GetChildren()) do
if v:IsA("Player") and v ~= player then
local isHit = (thrownHydrant.Position - v.Character.HumanoidRootPart.Position).Magnitude < 5
if isHit then
v:TakeDamage(50) -- Damage the player
v.Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
end
end
end
Debris:AddItem(thrownHydrant, 3.5) -- Remove after 3.5 seconds
end)
tween:Play()
-- Cooldown mechanism
player.Cooldown.Value = true
wait(3.5)
player.Cooldown.Value = false
-- Model reappears in hand for the next use
hydrant.Parent = player.Character
end
-- Function to equip the hydrant tool
local function onEquipped(tool)
local player = Players:GetPlayerFromCharacter(tool.Parent)
local audio = tool:WaitForChild("EquipSound")
audio:Play()
-- Create a cooldown value on the player
if not player:FindFirstChild("Cooldown") then
local cooldownValue = Instance.new("BoolValue")
cooldownValue.Name = "Cooldown"
cooldownValue.Value = false
cooldownValue.Parent = player
end
-- Connect usage of the hydrant tool
tool.Activated:Connect(function()
if not player.Cooldown.Value then
throwHydrant(player, tool, tool:WaitForChild("ThrowAnimation"), tool:WaitForChild("ThrowSound"))
tool:Destroy() -- Remove the tool from the player
end
end)
end
-- Main function to connect equip event
local function onToolInitialized(tool)
tool.Equipped:Connect(onEquipped)
end
-- Initialize the tool when created
local tool = script.Parent
onToolInitialized(tool)
Code Usage Example
Tool Setup: Create a fire hydrant tool model in Roblox with the necessary components:
- A sound instance named "EquipSound" for equipping the hydrant.
- An animation instance named "ThrowAnimation" to handle the throwing animation.
- A sound instance named "ThrowSound" for the throw effect.
Script Handling: Place the above script as a child of your fire hydrant tool in Roblox.
Execution: Equip the fire hydrant tool in-game to utilize the features. The player can throw it by clicking, which triggers audio, animations, and damage logistics.
Conclusion
This Lua script for a Roblox fire hydrant tool implements the desired functionality following best practices. It ensures smooth gameplay with audio, animations, hit detection, cooldown mechanics, and proper cleanup of objects. For more advanced training in scripting and game development, consider the courses available on the Enterprise DNA platform.
Description
This Lua script enables a fire hydrant tool in Roblox, featuring audio playback, custom animations, and collision handling. Players can throw the hydrant, dealing damage and triggering effects, all while managing cooldowns and object cleanup.