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

Why won't this explode?

Asked by 10 years ago

Needs fixing at line 50

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

local BLAST_RADIUS = 12
local BLAST_PRESSURE = 0

local RED_TEXTURE_ID = 'http://www.roblox.com/asset/?id=94691735'

--------------------
--| 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 ContentProviderService = Game:GetService('ContentProvider')

local TimeBomb = script.Parent

local BombMesh = WaitForChild(TimeBomb, 'Mesh')

local BoomSound = WaitForChild(TimeBomb, 'Boom')
local BeepSound = WaitForChild(TimeBomb, 'Beep')

local CreatorTag = WaitForChild(TimeBomb, 'creator')

local OriginalTextureId = BombMesh.TextureId

local Connection = nil

local ParentTorso = WaitForChild(TimeBomb, 'ParentTorso')
local XGraceDistance=2.5
local YGraceDistance=2.5

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

-- Returns the ancestor that contains a Humanoid, if it exists
local function FindCharacterAncestor(subject)
    if subject and subject ~= Workspace then
        local humanoid = subject:FindFirstChild('Humanoid')
        if humanoid then
            return subject, humanoid
        else
            return FindCharacterAncestor(subject.Parent)
        end
    end
    return nil
end

-- When a player is hit, if they are alive they will be tagged
local function OnExplosionHit(hitPart)
    if hitPart then
        local _, humanoid = FindCharacterAncestor(hitPart.Parent)
        if humanoid and humanoid.Health > 0 then
            humanoid:TakeDamage(50)
            end
        end
    end

-- Needless to say, this should only be called once
local function Explode()
    local explosion = Instance.new('Explosion')
    explosion.BlastRadius = BLAST_RADIUS
    explosion.BlastPressure = BLAST_PRESSURE
    explosion.Position = TimeBomb.Position
    explosion.Hit:connect(OnExplosionHit)
    explosion.Parent = Workspace

    BoomSound:Play()

    -- NOTE:
    -- If we just destroyed the bomb at this point, the boom sound would be destroyed too,
    -- so instead we will hide the bomb, keep it in the same spot, and schedule it for deletion

    TimeBomb.Transparency = 1
    TimeBomb.CanCollide = false
    TimeBomb.Anchored = true
    DebrisService:AddItem(TimeBomb, 3)
end

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

--Distance check to see if can collide should be true
Spawn(function()
    while not TimeBomb.CanCollide and ParentTorso.Value do
        local TorsoPosition = ParentTorso.Value.CFrame.p
        local XZOffset = TorsoPosition-Vector3.new(TimeBomb.CFrame.p.x,TorsoPosition.y,TimeBomb.CFrame.p.z)
        if XZOffset.magnitude>XGraceDistance or math.abs(TorsoPosition.y-TimeBomb.CFrame.p.y)>YGraceDistance then
            TimeBomb.CanCollide=true
        end
        wait(1/30)
    end
end)

-- Flash red and beep increasingly fast for about 3 seconds
local tickTime = 0.4
local red = true
repeat
    BombMesh.TextureId = red and RED_TEXTURE_ID or OriginalTextureId
    BeepSound:Play()
    wait(tickTime)
    red = not red
    tickTime = tickTime * 0.9
until tickTime < 0.1

Explode()

Answer this question