Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

2 comments

0

It works, but now when I unequip and equip again I get less ammo.

0

how much ammo do you lose?

This discussion is preserved as it was. New answers, comments, and votes are closed.