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

When I unequip and equip my gun I get less ammo and get an error, can someone help me?

So I have two problems with my script first of all I get an error. The error always happens when I unequip and equip the gun again. The error says "This Mouse is no longer active". The second problem is when I unequip and equip the gun again, I get less ammo. I would be happy if you could help me error:    ~~~~~~~~~~~~~~~~~

This Mouse is no longer active - Client - MainScript:24    Stack Begin - Studio   Script 'Players.FireTap1.Backpack.Deagle.MainScript', Line 24 - Studio - MainScript:24     Stack End




code:
	~~~~~~~~~~~~~~~~~
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
	local starterGui = game:GetService("StarterGui")
	
	script.Parent.Equipped:Connect(function(Mouse)
	textLabel.Visible = true
	local function reload()
		mouse = Mouse
					
			        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.reload:Play()
			            script.Parent.gunShot:Stop()
			        end
		 
		        while wait() do
			            textLabel.Text = (ammo.. " / "..maxAmmo)
			        end
		    end)
	
	local tool = script.Parent
	local anim2 = Instance.new("Animation")
	local input = game:GetService("UserInputService")
	anim2.AnimationId = "6425472378"
	    input.InputBegan:Connect(function(key)
			if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then
				reload()		
				script.Parent.reload:Play()
			end
		end)
	end)

script.Parent.Unequipped:Connect(function(Mouse)
	mouse = nil
	textLabel.Visible = false
end)

1 answer

First, you should organize your script a bit more to make it easier to see where it could've gone wrong. Second of all, NEVER use ":Connect()" inside of another ":Connect()": you risk creating far more errors, crashing, and much more because you're basically saying "run an instance of this function when you run" and unless you disconnect it, your running it multiple times.

Now, here's a cleaner version of your code (hopefully less buggy) with a few comments:

input = game:GetService("UserInputService");									--Gets input service
starterGui = game:GetService("StarterGui");										--Gets the starter GUI
player = game:GetService("Players").LocalPlayer;								--Gets the player
playerGui = player:WaitForChild("PlayerGui");									--Gets the player's GUI display
textLabel = playerGui:WaitForChild("AmmoDisplay"):FindFirstChild("AmmoText");	--Gets the player's current ammo text

maxAmmo	  = 8;			--The maximum amount of ammo the player can carry
ammo	  = maxAmmo;	--The current amount of ammo the player is carrying
reloading = false;		--If the player is currently reloading
eqquiped  = false;		--So then reloading doesn't run when the gun isn't eqquiped


reload = function()
	reloading = true;	--Set to the reloading state
	wait(1);			--Pause before reloading all ammo
	ammo = maxAmmo;		--Reloads all current ammo
	reloading = false;	--Set to not reloading
end --Reloads the weapon

activate = function(Mouse)
	if (ammo > 0 and not reloading) then	--If there's ammo and the player isn't reloading
		ammo -= 1;															--Takes away a bullet
		script.Parent.gunShot:Play();										--Plays a gunshot noise
if (Mouse.Target) then --Checks if it is even hitting anything
		if Mouse.Target.Parent:FindFirstChild("Humanoid") then				--If a "humanoid" is found within the hit object
			script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 20);	--Reqeust to deal damage
		end
end
	elseif not reloading then				--If not reloading (yet ammo is not greater than 0)
		reload();															--Reload the weapon
		script.Parent.reload:Play();										--Play the reloading noise
		script.Parent.gunShot:Stop();										--Stop any existing gunshot noise
	end
end

equippedEvent = script.Parent.Equipped:Connect(function(Mouse)
	eqquiped = true;							--Eqquiped
	textLabel.Visible = true;					--Displays current ammo
	--local tool = script.Parent; (Why is this needed?)
	--local anim2 = Instance.new("Animation"); (Your code isn't doing anything with this so just put it in yourself)
	--anim2.AnimationId = "6425472378"; (Just so you know, AnimationController:LoadAnimation() and Humanoid:LoadAnimation() is depricated; search for "AnimationController" to see the new changes)
	
	script.Parent.Activated:Connect(activate);	--Binds the activate function upon tool actiavation
end)

script.Parent.Unequipped:Connect(function(Mouse)
	eqquiped = false;							--No longer eqquiped
	equippedEvent:Disconnect(); 					--Unbinds the activation upon tool activation (the function no longer gets run when the tool is activated)
	textLabel.Visible = false;					--Removes the current ammo display
end)

input.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo and eqquiped then
		reload();						--Reloads the weapon
		script.Parent.reload:Play();	--Plays reloading noise
	end
end)

updateText = coroutine.wrap(function()
	while wait() do	--Pause to prevent freezing
		textLabel.Text = (ammo.. " / "..maxAmmo);	--Update text
	end
end) --Coroutines are like a seperate script: they run individually without having to wait for it to finish
updateText(); --Run the coroutine (I'd also reccomend using "RunService" instead of this as you would only need to update the text every frame)

Make sure to read it throughly, and compare it to your original code! It'll help you understand your mistakes!

If you have any questions or issues, contact me. ;)

6 comments

0

Now I get an error on line 25 the error is:Players.FireTap1.Backpack.Deagle.MainScript:25: attempt to index nil with 'Target'

0

And I also get another error- on line 47: Players.FireTap1.Backpack.Deagle.MainScript:47: attempt to index function with 'Disconnect'

0

Two more things firstly I can't damage anyone anymore and when I unequip and equip again I get less ammo

0

The "nil" comes from looking at something without hitting an object (looking off into the distance). Ad an "if" to check for a target (if Mouse.Target) and then the following code.

0

For the "Disconnect" issue, it's because I forgot to assign the "connect" function to a variable (I shouldn't be using "Disconnect" for the function itself) so around line 42 say "someName = theConnectFunction" and then replace line 47 with "someName:Disconnect()"

0

For your "dealing damage" issue, you should probably take a look at your other script. It's probably doing something wrong.

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