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

How would I make this rocket launcher script NOT destroy terrain?

Asked by 10 years ago

The current game I am making has terrain water. I was wanting to know if it was possible if you could have a rocket launcher that does no terrain damage, AND if it's possible to make the rocket launcher only do health damage, and not make everything fall apart. Oh, and if it's possible to only make the "swoosh" sound come on only once. I have a rocket launcher shot, but it repeats itself, and it gets very annoying.

Here's the code:

local Tool = script.Parent
local Launcher = Tool.Handle

local Rocket = Instance.new("Part")
Rocket.Locked = true
Rocket.BackSurface = 3
Rocket.BottomSurface = 3
Rocket.FrontSurface = 3
Rocket.LeftSurface = 3
Rocket.RightSurface = 3
Rocket.TopSurface = 3
Rocket.Size = Vector3.new(1, 2.5, 1)
Rocket.BrickColor = BrickColor.new(23)
Rocket.FormFactor = 3


local rocketMesh = Instance.new("SpecialMesh")
rocketMesh.MeshId = "http://www.roblox.com/asset/?id=31601976"
rocketMesh.TextureId = "http://www.roblox.com/asset/?id=31601599"
rocketMesh.Parent = Rocket

local debris = game:GetService("Debris")

local swooshSound
local explosionSound

local vCharacter
local vPlayer

function blow(hit, missile)
    if missile  == nil then return end
    if swooshSound then swooshSound:stop() end
    explosion = Instance.new("Explosion")
    explosion.Position = missile.Position
    -- find instigator tag
    local creator = missile:FindFirstChild("creator")
    explosion.Hit:connect(function(part, distance) onPlayerBlownUp(part, distance, creator) end)
    explosion.Parent = game.Workspace   
    if explosionSound then explosionSound:Play() end
    wait(.1)
    if missile then missile:Remove() end
end

function onPlayerBlownUp(part, distance, creator)
    if part.Name == "Head" or part.Name == "Torso" then 
        local humanoid = part.Parent.Humanoid
        tagHumanoid(humanoid, creator)
    end
end

function tagHumanoid(humanoid, creator)
    -- tag does not need to expire if all explosions lethal 
    if creator ~= nil then
        local new_tag = creator:clone()
        new_tag.Parent = humanoid       
    end
end

function fire(vTarget)
    local vCharacter = Tool.Parent  
    local vHandle = Tool:findFirstChild("Handle")
    if vHandle == nil then
        print("Handle not found")
        return 
    end
    local direction = vTarget - vHandle.Position
    direction = computeDirection(direction)
    local missile = Rocket:clone()
    local pos = vHandle.Position + (direction * 10.0)       
    missile.CFrame = CFrame.new(pos,  pos + direction) * CFrame.Angles(math.pi/2, 0, 0)

    local creator_tag = Instance.new("ObjectValue")

    local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

    if vPlayer == nil then
        print("Player not found")
    else
        if (vPlayer.Neutral == false) then -- nice touch
            missile.BrickColor = vPlayer.TeamColor
        end
    end

    local floatForce = Instance.new("BodyForce")
    floatForce.force = Vector3.new(0, missile:GetMass() * 196.1, 0.0)
    floatForce.Parent = missile

    missile.Velocity = direction * 200.0

    creator_tag.Value = vPlayer
    creator_tag.Name = "creator"
    creator_tag.Parent = missile    

    missile.Parent = game.Workspace

    if swooshSound then swooshSound:Play() end

    missile.Touched:connect(function(hit) blow(hit, missile) end)

    debris:AddItem(missile, 100.0)
end

function computeDirection(vec)
    local lenSquared = vec.magnitude * vec.magnitude
    local invSqrt = 1 / math.sqrt(lenSquared)
    return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

Tool.Enabled = true
function onActivated()
    if not Tool.Enabled then
        return
    end

    Tool.Enabled = false

    local character = Tool.Parent;
    local humanoid = character.Humanoid
    if humanoid == nil then
        print("Humanoid not found")
        return 
    end

    swooshSound = Launcher:FindFirstChild("Swoosh")
    explosionSound = Launcher:FindFirstChild("Explosion")

    local targetPos = humanoid.TargetPoint

    fire(targetPos)

    wait(0.5)

    Tool.Enabled = true
end


script.Parent.Activated:connect(onActivated)


1 answer

Log in to vote
0
Answered by
Link43758 175
10 years ago

Change the ExplosionType of the explosion to NoCraters. Hope I've helped!

local Tool = script.Parent
local Launcher = Tool.Handle

local Rocket = Instance.new("Part")
Rocket.Locked = true
Rocket.BackSurface = 3
Rocket.BottomSurface = 3
Rocket.FrontSurface = 3
Rocket.LeftSurface = 3
Rocket.RightSurface = 3
Rocket.TopSurface = 3
Rocket.Size = Vector3.new(1, 2.5, 1)
Rocket.BrickColor = BrickColor.new(23)
Rocket.FormFactor = 3


local rocketMesh = Instance.new("SpecialMesh")
rocketMesh.MeshId = "http://www.roblox.com/asset/?id=31601976"
rocketMesh.TextureId = "http://www.roblox.com/asset/?id=31601599"
rocketMesh.Parent = Rocket

local debris = game:GetService("Debris")

local swooshSound
local explosionSound

local vCharacter
local vPlayer

function blow(hit, missile)
    if missile  == nil then return end
    if swooshSound then swooshSound:stop() end
    explosion = Instance.new("Explosion")
    explosion.ExplosionType = Enum.ExplosionType.NoCraters
    explosion.Position = missile.Position
    -- find instigator tag
    local creator = missile:FindFirstChild("creator")
    explosion.Hit:connect(function(part, distance) onPlayerBlownUp(part, distance, creator) end)
    explosion.Parent = game.Workspace   
    if explosionSound then explosionSound:Play() end
    wait(.1)
    if missile then missile:Remove() end
end

function onPlayerBlownUp(part, distance, creator)
    if part.Name == "Head" or part.Name == "Torso" then 
        local humanoid = part.Parent.Humanoid
        tagHumanoid(humanoid, creator)
    end
end

function tagHumanoid(humanoid, creator)
    -- tag does not need to expire if all explosions lethal 
    if creator ~= nil then
        local new_tag = creator:clone()
        new_tag.Parent = humanoid       
    end
end

function fire(vTarget)
    local vCharacter = Tool.Parent  
    local vHandle = Tool:findFirstChild("Handle")
    if vHandle == nil then
        print("Handle not found")
        return 
    end
    local direction = vTarget - vHandle.Position
    direction = computeDirection(direction)
    local missile = Rocket:clone()
    local pos = vHandle.Position + (direction * 10.0)       
    missile.CFrame = CFrame.new(pos,  pos + direction) * CFrame.Angles(math.pi/2, 0, 0)

    local creator_tag = Instance.new("ObjectValue")

    local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

    if vPlayer == nil then
        print("Player not found")
    else
        if (vPlayer.Neutral == false) then -- nice touch
            missile.BrickColor = vPlayer.TeamColor
        end
    end

    local floatForce = Instance.new("BodyForce")
    floatForce.force = Vector3.new(0, missile:GetMass() * 196.1, 0.0)
    floatForce.Parent = missile

    missile.Velocity = direction * 200.0

    creator_tag.Value = vPlayer
    creator_tag.Name = "creator"
    creator_tag.Parent = missile    

    missile.Parent = game.Workspace

    if swooshSound then swooshSound:Play() end

    missile.Touched:connect(function(hit) blow(hit, missile) end)

    debris:AddItem(missile, 100.0)
end

function computeDirection(vec)
    local lenSquared = vec.magnitude * vec.magnitude
    local invSqrt = 1 / math.sqrt(lenSquared)
    return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

Tool.Enabled = true
function onActivated()
    if not Tool.Enabled then
        return
    end

    Tool.Enabled = false

    local character = Tool.Parent;
    local humanoid = character.Humanoid
    if humanoid == nil then
        print("Humanoid not found")
        return 
    end

    swooshSound = Launcher:FindFirstChild("Swoosh")
    explosionSound = Launcher:FindFirstChild("Explosion")

    local targetPos = humanoid.TargetPoint

    fire(targetPos)

    wait(0.5)

    Tool.Enabled = true
end


script.Parent.Activated:connect(onActivated)
0
It works, but it causes the Rocket Launcher to glitch with the sound. It loops. :/ TheRings0fSaturn 28 — 10y
0
That's definitely something not relating to what I've changed. Link43758 175 — 10y
0
Also, how would I make the explosion do 5 damage instead of the missile? It doesn't do anything to the surrounding enemies. TheRings0fSaturn 28 — 10y
0
Whoops, I wasn't thinking straight. Use the updated script. Link43758 175 — 10y
0
Alright. About the damage for the explosion, now. TheRings0fSaturn 28 — 10y
Ad

Answer this question