Problem lag with my animation

robloxapp-20240626-1242294.wmv (1.6 MB)

my code

local punchTool = script.Parent

local PunchAnimations = {
	{
		AnimationId = "rbxassetid://18216156939",
		Part = "Right Arm"
	},
	
	{
		AnimationId = "rbxassetid://18216154579",
		Part = "Right Arm"
	},
	
	{
		AnimationId = "rbxassetid://18216532641",
		Part = "Left Arm"
	}
}

local i = 0

local canAttack = true

local function getAnimation()
	i = i % #PunchAnimations + 1
	
	return PunchAnimations[i]
end

local connect: RBXScriptConnection

punchTool.Activated:Connect(function()
	local char = script.Parent.Parent:: Model
	local humanoid = char:FindFirstChild("Humanoid"):: Humanoid
	local animator = humanoid:FindFirstChild("Animator"):: Animator
	
	if canAttack then
		local punch = getAnimation()
		
		local animation = Instance.new("Animation")
		local animTrack
		
		animation.AnimationId = punch.AnimationId
		
		animTrack = animator:LoadAnimation(animation)
		
		if not animTrack.IsPlaying then
			canAttack = false

			animTrack:Play()
			
			connect = char[punch.Part].Touched:Once(function(hit: BasePart)
				local otherChar = hit.Parent
				local otherHumanoid = otherChar:FindFirstChild("Humanoid"):: Humanoid
				
				if otherHumanoid ~= humanoid then
					otherHumanoid:TakeDamage(10)
				end
			end)

			animTrack.Stopped:Wait()
			
			animation:Destroy()
			connect:Disconnect()

			canAttack = true
		end
	end
end)
1 Like

You didn’t really provide any information, so neither I or anyone else have any idea how to approach helping you. Please follow this post as a guideline for how to make threads in this category.

bro thanks for your reply but there is a video ^^^!!!

Yes- your video does absolutely nothing to help me understand what the problem is. I don’t know what it’s supposed to look like or why what I’m seeing is wrong. If “lag with my animation” is all I have to go off of, there’s nothing I can do to help you.

Again, I greatly recommend reading the post I mentioned so that you can have more insight of what information you should provide.