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.
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
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);
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
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)