As you can see if I have health that is less than max health (150 hp) the animation and subtracting health happens to early and sometimes repeats the animation
remoteevent.OnServerEvent:Connect(function(player,Boxz)
local d = false
local Selected = player:WaitForChild("Selected")
local Humanoid = WorkSpace:WaitForChild(player.Name).Humanoid
local Track = Humanoid:LoadAnimation(script.Animation)
local Box = WorkSpace.Boxes:WaitForChild(tostring(Boxz))
Selected.Value = tostring(Boxz)
local rootPartPos = Humanoid.RootPart.Position
local offsetDirection = rootPartPos-Box.PrimaryPart.Position
local Health = Box.Part.Health
Humanoid:MoveTo(Box.PrimaryPart.Position +offsetDirection.Unit*4)
Humanoid.MoveToFinished:Wait()
d = true
Selected.Changed:Connect(function()
Track:Stop()
end)
Health.Changed:Connect(function()
end)
spawn(function()
repeat
if Selected.Value == tostring(Box) and d == true then
Track:Play()
Box.Part.Health.Value -= 10
end
wait(1)
until Humanoid.MoveDirection.X > 0 or Humanoid.MoveDirection.Z > 0 or Humanoid.MoveDirection.X < 0 or Humanoid.MoveDirection.Z < 0 or Box.Part.Health.Value == 0 or Box.Part.Health.Value < 0
end)
end)