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

Why is the model moving everywhere before it comes to the right spot?

Asked by 7 years ago

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?

game.ReplicatedStorage.startmove.OnClientEvent:connect(function(movepart)
object = workspace:FindFirstChild(game.Players.LocalPlayer.Name.."Tycoon"):FindFirstChild(movepart)
enabled = true  
end)
game.Players.LocalPlayer:GetMouse().Move:connect(function()
    closestdist = math.huge
    print(enabled)
    if enabled == true then
        print('top')
    for i,v in ipairs(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
        closestdist = (target - v.Position).magnitude
        closestgrid = v
        print(closestgrid)
    x = v.Position.x
    y = v.Position.y
    z = v.Position.z
    print('middle')
        if object ~=nil then
            enabled = true
            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(.1) until object:GetPrimaryPartCFrame() == CFrame.new(x,y,z)
            enabled = true
            print(enabled)
        end
        else
            print('Grid Pos Failure')
        end
        end
    end
    end
end)
0
If possible, do this all on the client? Remotes = latency isses. Goulstem 8144 — 7y

1 answer

Log in to vote
0
Answered by 6 years ago

I recommend that you do all the moving around on the client so as to create less latency and don't max out the remotes limit. Then when it comes time to create the object or whatever it is into the workspace, use a remote event/function (whatever you deem best) to create that item. Then you will be saving a ton of bandwidth and causing the client less lag. Hope this helps and have a great day scripting!

Ad

Answer this question