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

After unequipping tool, script.Parent.Activated:Connect executes twice?

Asked by 4 years ago

WARNING: VERY CRAPPY CODE!

Whenever I unequip my gun (not dropping) and re-equip, it works normally except for

script.Parent.Activated:Connect(function() @ (Line 81)

The line executes multiple times for a reason I cant explain. Here is my local script:

-- Preference Locals
local MaxAmmo = 1
local ShootDelay = 0.2
local MaxCamShake = 1
local ReloadTime = 7.5
-- Main Locals
local Unequipped = false
local Ammo = MaxAmmo
local Reloading = false
local MuzzleOn = false
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid")
local Mouse = game.Players.LocalPlayer:GetMouse()
local camera = workspace.CurrentCamera
local counter = 0


--Animation Locals
local AnimationStatus = "Off"
local animationOn = script.Parent:WaitForChild('On')
local On = humanoid:LoadAnimation(animationOn)

--Main Stuff lol
--I suck at coding but ill optimize this crap latrrrr
--here i am procrastinating by adding useless green text thats probably gonna be removesd later
local function nowits6rlly()
    if AnimationStatus == "On" then
        On:Play()
        On.Looped = true
    elseif AnimationStatus == "Off" then
        On:Stop()
        On.Looped = false
    end
end

local function CreateMotor6D()
    local PlayerGunConnect = Instance.new("Motor6D")
    PlayerGunConnect.Part0 = player.Character:WaitForChild("Right Arm")
    PlayerGunConnect.Part1 = script.Parent.MainAssembly
    PlayerGunConnect.C0 = CFrame.new(0,-3.5,-0.63)
    PlayerGunConnect.C1 = CFrame.Angles(math.rad(90), 0, math.rad(180))
    PlayerGunConnect.Parent = script.Parent.Motor6D
end

local function DestroyMotor6D()
    local test = script.Parent.Motor6D:FindFirstChild("Motor6D")
    if test then
        script.Parent.Motor6D.Motor6D:Destroy()
    end
end

local function CamShake()
    for i = 1,10 do
        wait(0.025)
        char.Humanoid.CameraOffset = Vector3.new(math.random(-MaxCamShake,MaxCamShake), math.random(-MaxCamShake,MaxCamShake), math.random(-MaxCamShake,MaxCamShake))
    end
    char.Humanoid.CameraOffset = Vector3.new(0,0,0)
end

script.Parent.Equipped:Connect(function()
    CreateMotor6D()
    AnimationStatus = "On"
    nowits6rlly()
    MuzzleOn = false
    script.Parent.MuzzleFlash:FireServer(MuzzleOn)
    Mouse.Icon = "rbxasset://textures/GunCursor.png"
    local function Reload()
        Reloading = true
        Mouse.Icon = "rbxasset://textures/GunWaitCursor.png"
        script.Parent.RELOAD:FireServer()
        local animationReload = script.Parent:WaitForChild('Reload')
        local Reload = humanoid:LoadAnimation(animationReload)
        Reload.Looped = false
        Reload:Play()
        wait(ReloadTime)
        Ammo = MaxAmmo
        Reloading = false
        Mouse.Icon = "rbxasset://textures/GunCursor.png"
    end
    script.Parent.Activated:Connect(function()
        if Ammo>0 and not Reloading and counter==0 or counter==1 then
            Ammo=Ammo-1
            script.Parent.RayCastEvent:FireServer(script.Parent.BulHol.CFrame.Position, Mouse.Hit.Position)
            local animationShoot = script.Parent:WaitForChild('Shoot')
            local Shoot = humanoid:LoadAnimation(animationShoot)
            Shoot:Play()
            MuzzleOn = true
            script.Parent.MuzzleFlash:FireServer(MuzzleOn)
            CamShake()
            MuzzleOn = false
            script.Parent.MuzzleFlash:FireServer(MuzzleOn)
            if Unequipped == true then
                counter = 1
            end
        elseif not Reloading and Ammo==0 and counter==0 then
            print("reload")
            Reload()
        elseif counter==1 then
            counter=0 --My attempt at fixing the bug
        end
    end)
    print(counter .. "yes")--Some print()'s are my way of finding the bug.
    print(Ammo)
end)

script.Parent.Unequipped:Connect(function()
    print("unequipped, ammo is at  " .. Ammo .. "  bullet(s)")--Like this one
    DestroyMotor6D()
    Mouse.Icon = "rbxasset://SystemCursors/Arrow"
    AnimationStatus = "Off"
    nowits6rlly()
    Unequipped = true
end)
0
Please accept the answer that works User#30567 0 — 4y
0
sorry needed to check if it works rookiecookie153 53 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

Instead of having :Activated in the equipped event, put it as its own event, outside of the equipped event!

0
oh thx rookiecookie153 53 — 4y
0
accept my answerrr DragonSpawner12 61 — 4y
0
kk rookiecookie153 53 — 4y
Ad

Answer this question