Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I fixed this laggy script, Am I not using loops correctly?

Asked by 7 years ago

Is it inefficient? It works for like 2 seconds, and then it is just choppy and laggy. What is causing this?

enabled = false
game.Players.LocalPlayer:GetMouse().Move:connect(function()
    if enabled == true then
        closestdist = math.huge
    for i,v in pairs(workspace:FindFirstChild(game.Players.LocalPlayer.Name.."Tycoon"):FindFirstChild("Grid"):GetChildren()) do
        v.Name = i
    target = game.Players.LocalPlayer:GetMouse().Hit.p
    if (target - v.Position).magnitude < closestdist then
        closestgrid = v
        print(closestgrid)
        closestdist = (target - v.Position).magnitude
    x = v.Position.x
    y = v.Position.y
    z = v.Position.z
    end
        if object ~=nil then
            if enabled == true then

                enabled = false
            for o=0,1,.2 do wait()
                object:SetPrimaryPartCFrame(object:GetPrimaryPartCFrame():lerp((CFrame.new(x,y,z)),o))
            end
            repeat wait() until object:GetPrimaryPartCFrame() == CFrame.new(x,y,z)
            enabled = true
        end
        --object:MoveTo(Vector3.new(v.Position.x, v.Position.y, v.Position.z))
        --objectname = object.Name
        --game.ReplicatedStorage.placeitem:FireServer(x,y,z, objectname)
        else
            print('Grid Pos Failure')
                end
    end
    end
end)
0
Try adding a wait() to the end and see if it works better.. 4D_X 118 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

bump!

Ad

Answer this question