Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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?

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)

1 comment

0

If possible, do this all on the client? Remotes = latency isses.

1 answer

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.