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

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

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)

1 comment

0

Try adding a wait() to the end and see if it works better..

1 answer

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