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

I get an error in my gun when I unequip and equip it. How can I Fix This?

Asked by 3 years ago
Edited 3 years ago

I have been making a gun for my game and I get this error. The error always happens when I unequip and equip the gun again.

The error: ****This Mouse is no longer active ****

It is in line 20

My Script:

    local maxAmmo = 8
    local ammo = maxAmmo
    local reloading = false
    local player = game:GetService("Players").LocalPlayer
    local playerGui = player:WaitForChild("PlayerGui")
    local textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText")

    script.Parent.Equipped:Connect(function(Mouse)
        local function reload()
            reloading = true
            wait(1)
            ammo = maxAmmo
            reloading = false
        end

        script.Parent.Activated:Connect(function()
            if ammo > 0 and not reloading then
                ammo = ammo -1 
                script.Parent.gunShot:Play()
                if Mouse.Target.Parent:FindFirstChild("Humanoid") then
                script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
                end
                elseif reloading == false then
                reload()
                script.Parent.gunShot:Stop()
            end

            while wait() do
                textLabel.Text = (ammo.. " / "..maxAmmo)
            end
        end)

        local input = game:GetService("UserInputService")
        input.InputBegan:Connect(function(key)
            if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~=         maxAmmo then
                reload()
                textLabel.Text = "Out of ammo Press R to reload"
            end
        end)
    end)

    local Input = game:GetService("UserInputService") 
    Input.InputBegan:Connect(function(key)
        if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~=     maxAmmo then
            script.Parent.reload:Play()
        end
    end)

1 answer

Log in to vote
0
Answered by
P_4rio 30
3 years ago

You never defined "Mouse" in the second function where line 20 resides.

my solution was to create a variable named "mouse" and set it to nil when you equip the gun where you define "Mouse" I redefine "mouse" to equal to "Mouse"

local maxAmmo = 8
local ammo = maxAmmo
local reloading = false
local player = game:GetService("Players").LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText")
local mouse = nil

script.Parent.Equipped:Connect(function(Mouse)
    mouse = Mouse
    local function reload()
        reloading = true
        wait(1)
        ammo = maxAmmo
        reloading = false
    end

    script.Parent.Activated:Connect(function()
        if ammo > 0 and not reloading then
            ammo = ammo -1 
            script.Parent.gunShot:Play()
            if mouse.Target.Parent:FindFirstChild("Humanoid") then
            script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20)
            end
            elseif reloading == false then
            reload()
            script.Parent.gunShot:Stop()
        end

        while wait() do
            textLabel.Text = (ammo.. " / "..maxAmmo)
        end
    end)

    local input = game:GetService("UserInputService")
    input.InputBegan:Connect(function(key)
        if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~=         maxAmmo then
            reload()
            textLabel.Text = "Out of ammo Press R to reload"
        end
    end)
end)

local Input = game:GetService("UserInputService") 
Input.InputBegan:Connect(function(key)
    if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~=     maxAmmo then
        script.Parent.reload:Play()
    end
end)
0
It works, but now when I unequip and equip again I get less ammo. FireTap1 12 — 3y
0
how much ammo do you lose? P_4rio 30 — 3y
Ad

Answer this question