Damage For A Move is Broken

I am currently making a JoJo’s Bizarre Adventure Game on the ROBLOX Platform, as I’m finishing basic stand kit, I find myself a big if not giant problem, the barrage move for the Stand I’m making, after 1-hit, will always damage the player even if its not touching him, I tried multiple things to fix this glitch, but nothing worked, or it would deal 1 damage and stop, or deal the correct damage but the player would still get hurt even if the stand wasnt touching anything.

This is the part of the script that does the damage for the barrage move:

    myStand["Right Arm"].Touched:connect(function(otherPart)
	local StandBool = otherPart.Parent:FindFirstChild("StandBool")
	if StandBool then
	else
	
		pchar = otherPart.Parent
		hum = pchar:FindFirstChild("Humanoid")
		if pchar then
			local touch = myStand["Right Arm"]:GetTouchingParts()
			
			for i,v in pairs(touch)do
			if v then
				for i = 1,40 do
		if hum then
			if punchCooldown == false then
			hum:TakeDamage(1)
			punchCooldown = true
			local bodyVelocity = Instance.new("BodyVelocity") -- This is just for knockback.
			bodyVelocity.Parent = pchar.HumanoidRootPart
			bodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bodyVelocity.Velocity = char.Torso.CFrame.lookVector*5
			game.Debris:AddItem(bodyVelocity,0.1)
			punchCooldown = true
			wait(0.1)
			punchCooldown = false
			end
		end

I’m sorry if this script is messy but, any ideas on how to fix it and what is causing this glitch?

Tl;dr : the barrage move for my game when hits a npc/player, will continue doing damage until the move ends, even if the user isnt touching the target, the moment it hits it will continue damaging the target even after the player’s stand isn’t touching any NPC or Player, only stopping when the move does.

You might have to disconnect from the touched event.

local touchedEvent
 touchedEvent =     myStand["Right Arm"].Touched:connect(function(otherPart)
     --code here

     touchedEvent:Disconnect()
end)
1 Like

So you mean something like this or am I doing it wrong?
I’ve never used a disconnect event so all of this is new to me.

    if standPunching == true then
 local touchedEvent	
touchedEvent = myStand["Right Arm"].Touched:connect(function(otherPart)
	local StandBool = otherPart.Parent:FindFirstChild("StandBool")
	if StandBool then
	else
	
		pchar = otherPart.Parent
		hum = pchar:FindFirstChild("Humanoid")
		if pchar then
			local touch = myStand["Right Arm"]:GetTouchingParts()
			
			for i,v in pairs(touch)do
			if v then
				for i = 1,40 do
		if hum then
			if punchCooldown == false then
			hum:TakeDamage(1)
			punchCooldown = true
			local bodyVelocity = Instance.new("BodyVelocity")
			bodyVelocity.Parent = pchar.HumanoidRootPart
			bodyVelocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			bodyVelocity.Velocity = char.Torso.CFrame.lookVector*5
			game.Debris:AddItem(bodyVelocity,0.1)
			punchCooldown = true
			wait(0.1)
			punchCooldown = false
			end
		end
			end
			end	
			end
		else
			
			return false	
		end
		
			end
			
		
	
			touchedEvent:Disconnect()		
		end)

Or am I doing something wrong cause It’s still not working.

Hmm try disconnection outside the event

touchedEvent = myStand["Right Arm"].Touched:connect(function(otherPart)
     --code here

     
end)
touchedEvent:Disconnect()

The bug isn’t caused by the event not being disconnected, it’s just a simple logic error. You probably didn’t notice it because the poor indentation made it harder to follow the flow of the program.

Breaking the script into pseudocode:

--[[
Whenever the Right Arm touches a part:
    If the other part's parent contains a humanoid:
        Get all the parts the Right Arm is touching.
        For each one:
            If the part still exists:
                Repeat fourty times:
                    If there's still a humanoid, and the punchCooldown isn't active:
                            Damage the humanoid by 1, and knock them back.
                            Activate the punchCooldown.
                            Wait 0.1 seconds.
                            Deactivate the punchCooldown.
]]--

Looking at it like this, the issue is clear - after you enter the “repeat 40 times” loop, the script doesn’t do any more checks to see if the arm is still touching - the punches will stop if there’s no humanoid or the punchCooldown was active, but not if the arm stops touching the target.

The simplest fix would be to check again if the parts are touching every time you do one of the 40 punches - getting all the touching parts again, and seeing if the target is still in that list:

for i = 1,40 do
--New code start:
        local stillTouching = false
	local newTouchingParts = myStand["Right Arm"]:GetTouchingParts()
	for _, possibleMatch in pairs(newTouchingParts) do
		if possibleMatch == v then
			stillTouching = true
		end
	end
	if stillTouching then
--New code end.
		if hum then
			if punchCooldown == false then
				--The stuff in this block of code.
			end
		end
	end
end

This could get a bit laggy (iirc, GetTouchingParts is rather expensive), but since you’re only running this 10 times a second, for your purposes this would probably be fine.


While not directly relevant to the question, you will eventually need to disconnect the event, since after the “move” ends you don’t want the arm to keep dealing damage to the things it touches. You’ll need to disconnect the touchedEvent after the move begins (to stop it happening twice), and after a certain amount of time (to stop the move if it doesn’t hit anything). It’ll look something like this:

--When ability activated:
touchedEvent = myStand["Right Arm"].Touched:connect(function(otherPart)
	--if ability succesfully starts (after the punchCooldown check)
		if touchedEvent then
			touchedEvent:Disconnect() --Stops the ability from triggering again.
		end
end)
wait(4) --Or however long the ability is supposed to be active for.
if touchedEvent then
	touchedEvent:Disconnect() --Stops the ability from triggering if it never hit anyone.
end
2 Likes

This barrage thing I made was a Ctrl C + Ctrl V from the normal punch with LMB, that was my worst mistake, I’ll recode it, thanks a lot!