Why is the model moving everywhere before it comes to the right spot?
So, in this script it is supposed to locate the closest grid square to the mouse's position. But, the model goes to like 5 other grid squares before it arrives at the right one. How do I get it to immediately move to the closest grid square?
01 | game.ReplicatedStorage.startmove.OnClientEvent:connect( function (movepart) |
02 | object = workspace:FindFirstChild(game.Players.LocalPlayer.Name.. "Tycoon" ):FindFirstChild(movepart) |
05 | game.Players.LocalPlayer:GetMouse().Move:connect( function () |
06 | closestdist = math.huge |
08 | if enabled = = true then |
10 | for i,v in ipairs (workspace:FindFirstChild(game.Players.LocalPlayer.Name.. "Tycoon" ):FindFirstChild( "Grid" ):GetChildren()) do |
12 | target = game.Players.LocalPlayer:GetMouse().Hit.p |
13 | if (target - v.Position).magnitude < closestdist then |
14 | closestdist = (target - v.Position).magnitude |
23 | if enabled = = true then |
25 | for o = 0 , 1 ,. 2 do wait() |
26 | object:SetPrimaryPartCFrame(object:GetPrimaryPartCFrame():lerp((CFrame.new(x,y,z)),o)) |
28 | repeat wait(. 1 ) until object:GetPrimaryPartCFrame() = = CFrame.new(x,y,z) |
33 | print ( 'Grid Pos Failure' ) |