Body velocity not parenting the first time

In my game I have a mechanic to dodge and the system works after it is used the second time
I know the script runs since the animation plays and the system works after the second time

I print out the parent of the object and it SAYS that it is where it is supposed to be except the fact that it isn’t there

This may be a bug instead of a scripting error cause I don’t see anything I can fix

-- My entire code about this segment
function combat:Dodge(player)
	if player:GetAttribute("InCombat") == true then
		if emotes[player.UserId].IsPlaying == true then return end
		if player.Character == nil then return end
		if os.clock() - dodges[player.UserId].LastTick > 0.25 then
			dodges[player.UserId] = {LastTick = os.clock(), isDodging = true}
			player.Character.Humanoid.AutoRotate = false
			
			local direction = player.Character.Humanoid.MoveDirection * Vector3.new(1,0,1)
			if direction == Vector3.new(0,0,0) then
				direction = player.Character.HumanoidRootPart.CFrame.LookVector * Vector3.new(1,0,1)
			end
			
			local bodyVel = Instance.new("BodyVelocity")
			bodyVel.Parent = player.Character.PrimaryPart
			bodyVel.MaxForce = Vector3.new(math.huge,1,math.huge)
			local mass = 0
			
			for _,v in pairs(player.Character:GetChildren()) do
				if v:IsA("BasePart") then
					mass += v.Mass
				end
			end
			bodyVel.Velocity = direction * (mass * 2)
			
			local animation = playAnim.PlayAnim(player, dodgeAnims.Forward) -- this is a seperate module that works completely fine
			
			game:GetService("Debris"):AddItem(bodyVel, animation.Length) -- I moved this from the task.spawn and it changes nothing
			
			task.spawn(function()
				task.wait(animation.Length)
				dodges[player.UserId].isDodging = false
				player.Character.Humanoid.AutoRotate = true
			end)
		end
	end
end

PS: I waited for 5 seconds when making the instance and the same results appeared

So basically your code doesn’t work when you try to use it for the first time, but after that, it works fine?

Edit: try adding prints to the conditions where you do the return end just in case.

It works perfectly the second time

I doubt the returns at the top aren’t the problem since the animation still plays but not the moving part

can you check in the explorer if the body velocity is there? (I think the body velocity is being parented but its just not doing anything)

I checked the explorer and every time I do it
The bodyvelocity isn’t there for the first time except for every other time after that

try removing the task.spawn and just doing bodyVel:Destroy() after the player.Character.Humanoid.AutoRotate = true

Not working

What I think is that this is probably a bug that can’t be fixed on my end since the body velocity isn’t being shown on the character until the second time it works

The main problem is that the body velocity isn’t appearing on the character it looks like

can you print “Working” when the body velocity is first created to make sure its actually being created

100% being created

The script looks like it works fine until I get to the parenting part
even tried doing

repeat wait() until bodyVel.Parent == player.Character.PrimaryPart

and the code still continues regularly

can you do repeat wait() until not bodyVel then printing “Body Velocity Destroyed” to see when it is being destroyed?

I tried running the code you gave me and it didn’t end until I just put

until bodyVel.Parent == nil

I don’t know if that is relevant but it is being removed still even during the first time