Code Generator | Lua

Roblox Fire Hydrant Tool Script

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


Empty image or helper icon

Prompt

Here's the tool for roblox i want to make: Its a big ass fire hydrant you hold and you can throw it wherever you click. It should:-Play an audio when equipped -Ignore colission of the player that throws it to avoid any issues -Play a custom animation and sound when used -Be thrown to wherever you click when the animation is finished (also make the model the player holds dissapear) -If it hits another player, deal 50 damage and ragdolls -3.5 second cooldown before use again, model reappears on player's hand when the cooldown finishes and you can use it again. -The thrown hydrant dissapears in 3.5 seconds

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

  1. 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.
  2. Script Handling: Place the above script as a child of your fire hydrant tool in Roblox.

  3. 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.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

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.