The more I shoot, the more it fires

Okay, I know the title sounds kinda confusing, but what’s basically happening is that the first time when I shoot my gun, it works perfectly fine. But on the later ones it fires more and more until it is at a point to just 1 tap players which obviously nobody wants in their game. That’s why I need some help with this.

Code

Code removed LoL

(I did use FastCastRedux for this btw)

A bit of the code would be necessary…

1 Like

Oh whoops, I forgot to put in the code. My bad!

So, as you can see here, it keeps on firing more and more every time it hits a character.

I cant’t see anything wrong in that piece of code, can you show us the code where you are calling the :Fire() function?

1 Like

The problem isn’t there as well. In the picture there is a print which is printed from Main. So, the problem isn’t there. I also put another print at the top of the fire function which is “Fired from WeaponService” but that also doesn’t have the problem. The only problem is in the damaging if statement.

I think it is a problem outside of the module’s function because i don’t see any recursion occuring that would cause the :Fire() function to fire more and more as it goes on.

If you want to test this hypothesis for me, just put a print (‘Firing’) after this line

function WeaponService:Fire(mousePosition)

then send me the console

That’s what I said in the earlier post. The “Fired from WeaponService” was literally after that line which you said.

(I did send the picture of the console in the 2nd reply)

1 Like

Ohhh yes, my bad. I see what you mean now,

self.Caster.RayHit:Connect(function(_, result, _, bullet)
		local Hit = result.Instance;
		local Character = Hit:FindFirstAncestorWhichIsA("Model");

		if Character:WaitForChild("Humanoid") and Character:WaitForChild("Humanoid"):GetState() ~= Enum.HumanoidStateType.Dead then
			bullet:Destroy();
			print(Character:WaitForChild("Humanoid").Health);
			
			if Hit.Name == "Head" then
				Character:FindFirstChild("Humanoid"):TakeDamage(self.HeadDamage);
			else
				Character:FindFirstChild("Humanoid"):TakeDamage(self.Damage);
			end;
			
			print(Character:WaitForChild("Humanoid").Health);
		end;

		ExtraService:AddItem(bullet, 0.5); -- Alternative to Debris:AddItem(); Used task.delay() for this.
	end);

Change this to this:

local func; func = self.Caster.RayHit:Connect(function(_, result, _, bullet)
		local Hit = result.Instance;
		local Character = Hit:FindFirstAncestorWhichIsA("Model");

		if Character:WaitForChild("Humanoid") and Character:WaitForChild("Humanoid"):GetState() ~= Enum.HumanoidStateType.Dead then
			bullet:Destroy();
			print(Character:WaitForChild("Humanoid").Health);
			
			if Hit.Name == "Head" then
				Character:FindFirstChild("Humanoid"):TakeDamage(self.HeadDamage);
			else
				Character:FindFirstChild("Humanoid"):TakeDamage(self.Damage);
			end;
			
			print(Character:WaitForChild("Humanoid").Health);
		end;

		ExtraService:AddItem(bullet, 0.5); -- Alternative to Debris:AddItem(); Used task.delay() for this.
func:Disconnect()
	end);

Tell me if that changes anything.

1 Like

Oh my god, that fixed it! Thank you so much! Btw if you don’t mind, can you explain me what was the problem here?

1 Like

Sure thing,

The problem here is, you are creating an event connector without later disconnecting it.

self.Caster.RayHit:Connect(function(_, result, _, bullet)

obviously the bullet will only hit a player or wall once so after it does hit something, this function should be disconnected. Your function will keep firing everytime the rayobject hits anything, so overtime you are creating more and more events. This is the code equivelant to what is happening overtime:

-- WEAPON FIRED
self.Caster.RayHit:Connect(function(_, result, _, bullet) --CHECK FOR HIT
--WEAPON FIRED
self.Caster.RayHit:Connect(function(_, result, _, bullet) -- THIS STILL EXISTS FROM PREVIOUS WEAPON FIRED
self.Caster.RayHit:Connect(function(_, result, _, bullet) -- NEW EVENT CREATED

Hopefully i explained that well

1 Like

Ohhh, thanks a lot for the help mate! Have a great rest of you day!

1 Like

Memory leaks, whenever creating inner event connections within created outer event connections remember to disconnect those inner event connections otherwise whenever the outer connection is fired those inner connections will be created. Here’s a short example.

local part = script.Parent

part.Touched:Connect(function(hit1) --This connection is only made once.
	print(hit1)
	part.TouchEnded:Connect(function(hit2) --This connection is made each time its outer connection is fired.
		print(hit2)
	end)
end)
1 Like