Why does my movement system make the part go crazy for no reason

So for some strange reason my movement system starts going crazy at certain moment, I have no idea why, I have looked at other posts to try and fix, and they don’t work

-- just an rough draft
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local OriginInstance = workspace.RayCastOrigin
local BuildingPart = workspace.BuildPart
local ScaleInst = tonumber(script.Parent.ScaleBox.Text)
local Scale = 1

Mouse.TargetFilter = workspace.Camera

	while true do
		Mouse.TargetFilter = workspace.CurrentCamera
		local MousePosition = Mouse.hit.p
		OriginInstance.CFrame = CFrame.new(OriginInstance.Position, MousePosition)
		local Origin = OriginInstance.Position
		local Direction = OriginInstance.CFrame.LookVector * 1e5
		local Params = RaycastParams.new()
		Params.FilterDescendantsInstances = {workspace.BuildPart}
		Params.FilterType = Enum.RaycastFilterType.Blacklist

		local Result = workspace:Raycast(Origin, Direction, Params)
		if Result then
			local Position = Vector3.new(math.floor(Result.Position.X / Scale) * Scale, math.floor(Result.Position.Y / Scale) * Scale, math.floor(Result.Position.Z / Scale) * Scale)
			BuildingPart.Position = Position

My guess is due to the grid snapping. I would look into trying to only set the position when the mouse is moved. And your Origin really should be the player’s camera and not the part.

Also not for nothing but Mouse.hit.p + BuildingPart.Size.Y/2 will get you the position you need for the movement. You’re over complicating the script.

I would also opt to use RunService.Stepped instead of a while true do loop.

1 Like

Hmmm, this doesn’t seem to work, I tried removing the scale, and replacing the while loop with the event, it still didn’t seem to change anything, can you think of anything else?