(HELP) how do i make touched event calls NOT stack up every time it fires on a part?

so i have a fireball script and every time the fireball touches a part it gets destroyed, the problem is the amount of times the touch event is called, is increasing.
here is some of the code

local Debris = game:GetService("Debris")
local aa= 0
function Ember()
	if canAttack == false then
		return
	end
	local func
	aa= aa+1
	canAttack = false
	FireballAnim:Play()
	FireballAnim:GetMarkerReachedSignal("FireEmber"):Connect(function()
		NPC.RightHand.Hellfire:Play()
		local a = false
		FireballAnim:AdjustSpeed(0)
		local fireball = Instance.new("Part",workspace)
		fireball.Name = "fireball"..aa
		local att = Instance.new("Attachment",fireball)
		local lv = Instance.new("LinearVelocity", fireball)
		local av = Instance.new("AngularVelocity",fireball)
		av.Attachment0= att
		av.MaxTorque = 10000
		av.AngularVelocity = Vector3.new(0,0,1000)
		lv.Attachment0 = att
		lv.ForceLimitsEnabled = false
		lv.VectorVelocity = NPC.RightHand.CFrame.UpVector * -40
		fireball.CanCollide = false
		fireball.Size = Vector3.new(2,2,2)
		fireball.Transparency = 1
		fireball.Position = NPC.RightHand.Position
		local flame = NPC.RightHand.ParticleEmitter:Clone()
		flame.Parent = att
		flame.Enabled = true
		func = fireball.Touched:Connect(function(hit)
			if hit then
				print(fireball.Name)
				fireball:Destroy()
			end
		end)
		Debris:AddItem(fireball,5)
		wait(0.2)
		func:Disconnect()
		FireballAnim:AdjustSpeed(1)
	end)
	wait(6)
	canAttack = true

end

then i use

while wait() do
    ember()
end

fire time it prints “fireball1”
then it prints “fireball2 (x2)”
then it prints “fireball3 (x3)”
then it prints “fireball4 (x4)”
and so on. After a while my game starts to lag upon each hit. How do i make it print/hit only 1 time each time the part is being hit?

No, the issue is not what @soprosostupid said. Its the issue about understanding connections, post

Its because on a while loop you are constantly stacking up Connections of :GetMarkerReachedSignal("FireEmber")

Means the first time the loop called Ember() function it created 1 connection, the second time it creates a new connection without Disconnecting the previous one, So, 2 connections exist and it will fire both, creating 2 fire balls, and thats why you see that each new ball the hits increases.

You should disconnect the :GetMarkerReachedSignal("FireEmber") or only have one to handle entire mechanics

2 Likes

thank you it worked, for those who have the same issue i did

func = FireballAnim:GetMarkerReachedSignal("FireEmber"):Connect(function()
		NPC.RightHand.Hellfire:Play()
		local a = false
		FireballAnim:AdjustSpeed(0)
		local fireball = Instance.new("Part",workspace)
		fireball.Name = "fireball"..aa
		local att = Instance.new("Attachment",fireball)
		local lv = Instance.new("LinearVelocity", fireball)
		local av = Instance.new("AngularVelocity",fireball)
		av.Attachment0= att
		av.MaxTorque = 10000
		av.AngularVelocity = Vector3.new(0,0,1000)
		lv.Attachment0 = att
		lv.ForceLimitsEnabled = false
		lv.VectorVelocity = NPC.RightHand.CFrame.UpVector * -40
		fireball.CanCollide = false
		fireball.Size = Vector3.new(2,2,2)
		fireball.Transparency = 1
		fireball.Position = NPC.RightHand.Position
		local flame = NPC.RightHand.ParticleEmitter:Clone()
		flame.Parent = att
		flame.Enabled = true
		fireball.Touched:Connect(function(hit)
			if hit then
				print(fireball.Name)
				fireball:Destroy()
			end
		end)
		Debris:AddItem(fireball,5)
		wait(0.2)
		func:Disconnect()
		FireballAnim:AdjustSpeed(1)
	end)
	wait(6)
	canAttack = true
        func:Disconnect()
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.