Trouble with animation & damage script

Basically, I made a script that plays an animation for a tool, and if the hitbox in the tool hits a player, the hit player takes 10 damage and a remote event fires (has nothing to do with the issue i believe)

My script: (i know i could remove some functions and make it way shorter, but im happy as long as it works)

local alreadytouched = false
local cooldowntime = 1.5
local enabled = false

script.Parent.Equipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	local db = plr:FindFirstChild("ItemCooldown")
	print(plr.Name)
	script.Parent.Activated:Connect(function()
		if db.Value == false then
			db.Value = true
			local anim = script.Parent.Animation
			local anim2 = plr.Character:WaitForChild("Humanoid").Animator:LoadAnimation(anim)
			
			script.Parent.BodyAttach.Sound:Play()
			wait(0.1)
			anim2:Play()
			wait(0.5)
			alreadytouched = true
			script.Parent.Hitbox.Touched:Connect(function(hit)
				if alreadytouched == true then
					enabled = true
					local hmn = game.Players:GetPlayerFromCharacter(hit.Parent)
					if hmn then
						if enabled == true then
							hmn.Character:FindFirstChild("Humanoid"):TakeDamage(11)
							game.ReplicatedStorage.RemoteEvents.PlrPie:FireClient(hmn)
							alreadytouched = false
							plr:WaitForChild("leaderstats").SmashPoints.Value += 1
						elseif enabled == false then
							return					
						end
					else
						wait(0.1)
					end
				end
			end)
			anim2.Ended:Wait()
			wait(1.5)
			db.Value = false
			return
		elseif db.Value == true then		
			wait(1.5)
		end
	end)
	script.Parent.Unequipped:Connect(function()
		enabled = false
		alreadytouched = false
		return
	end)
end)

Please don’t just tell me my script is too long for what I’m actually trying to do, but please just tell me the issue if i’m not asking for too much :slightly_smiling_face:

I would gladly help, if you could explain the issue better.
Are there error messages or something not working???

Well, what is the issue?

Without context, we cannot help.

There are no error messages. I see now i forgot to explain my issue :rofl:

My issue is that it still does damage after the animation has played (ONLY if it didnt hit a player yet, so it basically does damage when it hits a player, even after the animation as ended, in which i am trying to make it stop)

Explained it above, I’m so sorry :sweat_smile:

Might be a bit picky but uh, you define cooldowntime and never use it.

1 Like

Yeah, I understand, it’s a really big script for such a small function. I’ll shorten it up once the issue is found. I always do this type of stuff, I probably just forgot.

You connect this and never disconnect it. Just do something like local connection = blabla:Connect() and then later when it’s served its purpose you can say connection:Disconnect()