'My object is glitching out because of the way I make it move

I'm working on a game with my friend, and we are making an object move around a house and go to random points. The thing is, if it moves higher up on the X axis then goes down then it starts shaking and acting weird.

        path:ComputeAsync(character.Position, workspace:WaitForChild("ModelsMain").SleepBag.SLEEP.Position)
        waypoints = path:GetWaypoints()
        for _, wp in pairs(waypoints) do
            local tweenInfo = TweenInfo.new(.6, Enum.EasingStyle.Linear)
            local tween = TweenService:Create(character, tweenInfo, {CFrame = CFrame.new(wp.Position.X, character.CFrame.Y, wp.Position.Z)})

            tween:Play()
            tween.Completed:Wait(2)

            --character.CFrame = CFrame.new(wp.Position.X, wp.Position.Y, wp.Position.Z)
        end
        
        wait(5)
        
        local baggy = game:GetService("ServerScriptService").moneybag:Clone()
        baggy.Parent = workspace:WaitForChild("Moneys")
        baggy.Position = character.Position

I am aware that character.CFrame.Y isn't the best but I can't really figure out how to keep it on the ground. (RayCasting?)

I have to use Tween because it's an object and it can't have a humanoid.

Here is a video. In this video, you can see the object shake and then when it reaches it's point then it'll drop "dead", as I call it.



Solution 1:[1]

One of the problems with this script is that the time for the tween to complete is inconsistent. Currently, it is setup to take 0.6 seconds to move to each waypoint. The issue is that the distance between the waypoints are not taken into account. If it is far away, the character will move very fast. If it is close, the character will move slower.

To fix this, either the speed of the tween should be based off of the distance from the character to the waypoint, or another method should be used to move the character such as humanoids (see other solutions below).

Another issue causing the bug could actually be the usage of character.CFrame.Y. This may interfere with the physics system which would cause very odd bugs related to movement. There are many different methods of fixing this.

One solution is to disable the physics while tweening, though that is hacky and would cause other issues later on. Another is to use a different method that avoids modifying the Y axis. Once again, it would probably be best to use a humanoid however if that is not an option see tweening an object along one axis.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GalaxyShard