I have this script that's meant to move a preview for placing something, and I tried to make it lerp when you move the preview (similar to miner's haven), but for some reason it instantly starts spazzing out, am I doing something wrong here?

MMC = mouse.Move:Connect(function()
        local x = clamp(mouse.Hit.p.X,GridMin.X,GridMax.X)
        local y = clamp(mouse.Hit.p.Y+Preview.Hitbox.Size.Y/2,GridMin.Y,64)
        local z = clamp(mouse.Hit.p.Z,GridMin.Z,GridMax.Z)
        
        if OldPos ~= Vector3.new(x,y,z) then
            spawn(function()
                local LerpStart = OldPos
                --Preview:SetPrimaryPartCFrame(CFrame.new(snapTo(Vector3.new(x,y,z),Grid)))
                
                for i=1,60 do
                    Preview:SetPrimaryPartCFrame(CFrame.new(LerpStart:Lerp(Vector3.new(x,y,z),i/60)))
                    wait(0.01)
                end
            end)
        end
        
        OldPos = Vector3.new(x,y,z)
    end)

3 comments

0

It's because the lerp is starting every time your mouse moves a pixel, resulting in a hundred lerp loops. A debounce might be what you are looking for.

0

Also CFrame.p is deprecated, use CFrame.Position. In other words you should be doing mouse.Hit.Position.X

0

I'm only running it when the snapped position changes (it's snapped to 1 stud)