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

script seemingly randomly triggering unequipped event when tool is equipped?

Asked by 5 years ago

hello. so, put simply, i have no idea what is wrong with this script. if the Equipped event is put before the Unequipped event, the equipped event works normally, but as a result the Unequipped event never fires. but, if the Unequipped event is before the equipped event, the function the Equipped event calls breaks, as a result of the function called in the Unequipped event being called before it for... some reason? i tried reproducing the effect in another tool, but it works perfectly fine there no matter the position of the equipped and unequipped events.

here is the offending code (sorry for the semi-wall):

function EndAnimation()
    local p = weapon_parts[1]:GetChildren()
    for i = 1, #p do
        if p[i].className == "Sound" then
        p[i]:Destroy()  
        end
    end 
    if motors[1] then
        motors[1].Part1 = character:findFirstChild("Left Arm")
    end
    if motors[2] then
        motors[2].Part1 = character:findFirstChild("Right Arm")
    end

    if welds[1] then
        welds[1]:remove()
        welds[1] = nil
    end
    if welds[2] then
        welds[2]:remove()
        welds[2] = nil
    end

    weapon_parts[1].Parent = nil
    if weapon_model then
        weapon_model.Parent = nil
    end
    coroutine.resume(coroutine.create(function()
        if weapon_model then
        local swm = weapon_model
        wait()
        swm.Parent = nil
        wait(0.1)
        swm.Parent =  nil
        wait(0.5)
        swm.Parent =  nil
        end
    end))   
end

--WeaponAnimation is the viewmodel reference
--WeaponObject is the world model (object that the character holds)

function StartAnimation()
    if humanoid.Health <= 0 then return end

    local check = {torso:findFirstChild("LeftWeld"), torso:findFirstChild("RightWeld")}
    if check[1] then check[1]:remove() end
    if check[2] then check[2]:remove() end

    local check2 = {character:findFirstChild("WeaponObject")}
    if check2[1] then check2[1].Parent = nil end
    if motors[1] then
        motors[1].Part1 = nil
    end

    welds = {Instance.new(conn_type), Instance.new(conn_type)}
    welds[1].Part0 = torso
    welds[2].Part0 = torso
    welds[1].Part1 = character:findFirstChild("Left Arm")
    welds[2].Part1 = character:findFirstChild("Right Arm")
    welds[1].Name = "LeftWeld"
    welds[2].Name = "RightWeld"
    welds[1].C0 = CFrame.new(-1.5, 0, 0)
    welds[2].C0 = CFrame.new(1.5, 0, 0)
    welds[1].Parent = torso
    welds[2].Parent = torso

    weapon_parts[1].Parent = character
    local wep_weld = Instance.new(conn_type)
    wep_weld.Part0 = character:findFirstChild("Right Arm")
    wep_weld.Part1 = weapon_parts[1]
    wep_weld.C0 = CFrame.new()
    wep_weld.Parent = weapon_parts[1]

    local weld1 = welds[1]
    local weld2 = welds[2]

    local r_serv = game:GetService("RunService")

    local move_anim_speed = 3
    local last_p = Vector3.new()
    local move_amm = 0
    coroutine.resume(coroutine.create(function()
        while weld1.Parent ~= nil do
            local delta = wait()
            local cur_p = torso.Position
            if (cur_p - last_p).magnitude >= 0.1 and humanoid.Jump == false then
                move_amm = math.min(1, move_amm + delta * move_anim_speed)
            else
                move_amm = math.max(0, move_amm - delta * move_anim_speed)
            end
            last_p = cur_p
        end
    end))

    local last_va = 0
    local last_va2 = 0
    local view_velocity = {0, 0}

    coroutine.resume(coroutine.create(function()
        wait()
        local last_time = tick()
        while weld1.Parent ~= nil do
            r_serv.Heartbeat:wait() ------------------------------------------------
            if humanoid.Health <= 0 then return end
            local delta = tick() - last_time
            last_time = tick()

            local breathe_amp = 3
            local breathe_freq = 1
            local breathe = math.sin(math.rad(tick() * 40 * breathe_freq)) * breathe_amp

            local shake_freq = 10
            local shake_amp = {0.15, 0.15}
            local arm_shake = CFrame.new(
                math.sin(math.rad(tick() * 45 * shake_freq)) * move_amm * shake_amp[1],
                0,
                math.abs(math.cos(math.rad(tick() * 45 * shake_freq)) * move_amm * shake_amp[2]))


            local anmtp = _G.AnimType
            local anmst = _G.AnimState

            local view_angle = 0 

            if anmst == nil then
                anmst = 0
            end

            if anmtp ~= nil then
                if Animations[anmtp] == nil then
                    anmtp = "Default"
                end
            else
                anmtp = "Default"
            end

            local curr_anim = PlayAnimation(anmtp, anmst) --left, right, weapon, wep trans

            local chestCF = CFrame.new(0, 0.5, 0) * CFrame.Angles(math.rad(math.max(-rot_amplitude_chest, math.min(rot_amplitude_chest, view_angle)) + 90 + breathe), 0, 0)
            weld1.C1 = (chestCF * curr_anim[1] * CFrame.new(0, -0.5, 0)):inverse()
            weld2.C1 = (chestCF * curr_anim[2] * CFrame.new(0, -0.5, 0)):inverse()
            wep_weld.C1 = (curr_anim[3] * CFrame.new(0,0,-0)):inverse()

        end
    end))

end

local last_st = 0
local eq = false

tool.Unequipped:connect(function()
    eq = false
    EndAnimation()
end)

tool.Equipped:connect(function()
    if eq then return end
    eq = true   
    StartAnimation()
end)
0
maybe use :Connect() instead of :connect() as that's deprecated? ihatecars100 502 — 5y

Answer this question