Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Shooting a rocket launcher on specific axis, how?

Asked by 7 years ago

Can anyone tell me how to make a script that shoots a rocket launcher on specific axis like on X Axis? Anyway here's the script I wanted to make. Any help would be appreciated, Thank you for your attention.

Note : I'm really sorry that I copied this on "Roblox Battle Rocket Launcher" I only wanted to edit it. I'm just a beginner scripter.

-----------------
--| Constants |--
-----------------

local COOLDOWN = 3.0 -- Seconds until tool can be used again
local COOLDOWN_JUGGERNAUT = 1.5
local COOLDOWN_ROCKETONLY = 2

-- RocketPropulsion Fields
local TARGET_RADIUS = 5
local MAX_SPEED = 60
local MAX_TORQUE = Vector3.new(4e6, 4e6, 0)
local MAX_THRUST = 50000
local THRUST_P = 500
local THRUST_D = 50000

local TARGET_OVERSHOOT_DISTANCE = 10000000

-- How far away from the handle does the user have to click for
-- the rocket to allow killing of the user themself.
local CLOSE_SHOT_DISTANCE = 10

-- Rocket Fields
local ROCKET_MESH_ID = 'http://www.roblox.com/asset/?id=94690081'
local ROCKET_MESH_SCALE = Vector3.new(2.5, 2.5, 2)
local ROCKET_PART_SIZE = Vector3.new(1, 1, 4)

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
    assert(parent, "ERROR: WaitForChild: parent is nil")
    while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
    return parent[childName]
end

-----------------
--| Variables |--
-----------------

local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService('Players')

local Tool = script.Parent
local ToolHandle = Tool.Handle

local RocketScript = WaitForChild(script, 'Rocket')

local SwooshSound = WaitForChild(script, 'Swoosh')
local BoomSound = WaitForChild(script, 'Boom')
local ReloadSound = WaitForChild(ToolHandle, 'ReloadSound')
local EquipSound = WaitForChild(ToolHandle, 'Equip')

local MyModel = nil
local MyPlayer = nil

local BaseRocket = nil
local RocketClone = nil

-----------------
--| Functions |--
-----------------

local function MakeBaseRocket()
    -- Set up the rocket part
    local rocket = Instance.new('Part')
    rocket.Name = 'Rocket'
    rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
    rocket.Size = ROCKET_PART_SIZE
    rocket.CanCollide = false
    rocket.BottomSurface = Enum.SurfaceType.Smooth
    rocket.TopSurface = Enum.SurfaceType.Smooth

    -- Add the mesh
    local mesh = Instance.new('SpecialMesh', rocket)
    mesh.MeshId = ROCKET_MESH_ID
    mesh.Scale = ROCKET_MESH_SCALE
    mesh.TextureId = ToolHandle.Mesh.TextureId

    -- Add fire
    local fire = Instance.new('Fire', rocket)
    fire.Heat = 5
    fire.Size = 2

    -- Add the propulsion
    local rocketPropulsion = Instance.new('RocketPropulsion', rocket)
    rocketPropulsion.CartoonFactor = 1
    rocketPropulsion.TargetRadius = TARGET_RADIUS
    rocketPropulsion.MaxSpeed = MAX_SPEED
    rocketPropulsion.MaxTorque = MAX_TORQUE
    rocketPropulsion.MaxThrust = MAX_THRUST
    rocketPropulsion.ThrustP = THRUST_P
    rocketPropulsion.ThrustD = THRUST_D

    -- Clone the sounds
    local swooshSoundClone = SwooshSound:Clone()
    swooshSoundClone.Parent = rocket
    local boomSoundClone = BoomSound:Clone()
    boomSoundClone.PlayOnRemove = true
    boomSoundClone.Parent = rocket

    -- Attach creator tags
    local creatorTag = Instance.new('ObjectValue', rocket)
    creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
    creatorTag.Value = MyPlayer
    local nameTag = Instance.new('StringValue', creatorTag)
    nameTag.Name = 'weaponName'
    nameTag.Value = Tool.Name
    local iconTag = Instance.new('StringValue', creatorTag)
    iconTag.Name = 'weaponIcon'
    iconTag.Value = Tool.TextureId

    -- The "close shot" tag. True if the rocket should be able to kill the
    -- creator.
    local closeShot = Instance.new('BoolValue', rocket)
    closeShot.Name = 'closeShot'
    closeShot.Value = false

    -- Finally, clone the rocket script and enable it
    local rocketScriptClone = RocketScript:Clone()
    rocketScriptClone.Parent = rocket
    rocketScriptClone.Disabled = false

    return rocket
end

local function OnEquipped()
    MyModel = Tool.Parent
    MyPlayer = PlayersService:GetPlayerFromCharacter(MyModel)
    BaseRocket = MakeBaseRocket()
    RocketClone = BaseRocket:Clone()
    EquipSound:Play()
end

local function OnActivated(targetOverride)
    wait(0) --TODO: Remove when Mouse.Hit and Humanoid.TargetPoint update properly on iPad
    if Tool.Enabled and MyModel and MyModel:FindFirstChild('Humanoid') and MyModel.Humanoid.Health > 0 then
        Tool.Enabled = false

        -- Pick a target
        local targetPosition = targetOverride or MyModel.Humanoid.TargetPoint

        -- Maybe set the "closeShot" flag
        print("CloseShot: ", (targetPosition - ToolHandle.Position).magnitude)
        if (targetPosition - ToolHandle.Position).magnitude < CLOSE_SHOT_DISTANCE then
            RocketClone.closeShot.Value = true
        end

        -- Position the rocket clone
        local spawnPosition = ToolHandle.Position + (ToolHandle.CFrame.lookVector * (ToolHandle.Size.Z / 2))
        RocketClone.CFrame = CFrame.new(spawnPosition, targetPosition) --NOTE: This must be done before assigning Parent
        DebrisService:AddItem(RocketClone, 30)
        RocketClone.Parent = game.Workspace

        -- Assign target and launch!
        local rocketPropulsion = RocketClone:FindFirstChild('RocketPropulsion')
        if rocketPropulsion then
            local direction = (targetPosition - RocketClone.Position).unit
            rocketPropulsion.TargetOffset = RocketClone.Position + (direction * TARGET_OVERSHOOT_DISTANCE)
            rocketPropulsion:Fire()
        end

        -- Prepare the next rocket to be fired
        RocketClone = BaseRocket:Clone()

        ReloadSound:Play()

        -- Cooldown
        wait(COOLDOWN)

        -- Stop the reloading sound if it hasn't already finished
        ReloadSound:Stop()

        Tool.Enabled = true
    end
end

local function OnUnequipped()
    ReloadSound:Stop()
end

-- Also activate when the Action Button is pressed
local function OnChildAdded(child)
    if child.Name == 'ActionButtonData' then
        child.Changed:connect(function(newValue)
            local bindable = child:FindFirstChild('GetTargetPosition')
            if bindable and string.sub(newValue, 1, 1) == 'v' then
                local matches = {}
                for match in string.gmatch(newValue, '%d+%.?%d*') do
                    table.insert(matches, match)
                end
                if #matches == 4 then
                    local screenPosition = Vector2.new(matches[1], matches[2])
                    local screenSize = Vector2.new(matches[3], matches[4])
                    local targetPosition = bindable:Invoke(screenPosition, screenSize, {MyModel})
                    OnActivated(targetPosition)
                end
            end
        end)
    end
end

--------------------
--| Script Logic |--
--------------------

Tool.Equipped:connect(OnEquipped)
Tool.Activated:connect(OnActivated)
Tool.Unequipped:connect(OnUnequipped)

-- Listen for Action Button Data Object
for _, child in pairs(Tool:GetChildren()) do
    OnChildAdded(child)
end
Tool.ChildAdded:connect(OnChildAdded)

Answer this question