Script causing immense lag and I don't know why

I am making a tool that allows the user to place parts wherever their mouse clicked. It worked fine until I added a few lines. I was trying to make the user unable to place parts if they were clicking to far away. So I added this

					local posit = round(mouse.Hit.p,4)
					local Distance = (posit - Vector3.new(RootPart.Position.X,RootPart.Position.Y,RootPart.Position.Z)).Magnitude
					if Distance <= 35 then 

This is causing immense lag for some reason. The game even crashes after a while. I don’t know what’s causing it as i don’t see anything looping. Here is the full script

local mouse = player:GetMouse()
local tool = script.Parent

tool.Equipped:Connect(function()
 equipped = true
end)

tool.Unequipped:Connect(function()
 equipped = false
end)

function round(vector, unit)
	return vector - Vector3.new(vector.X%unit, vector.Y%unit, vector.Z%unit)
end

while equipped == true do

mouse.Move:Connect(function()
	if player.Character:FindFirstChild("Blocks") then	
		print("foundsuccesful")
		if mouse.Target.ClassName == "Part" then
			print("partfound")

			local NewBlock = script.PreviewPlace:Clone()
			NewBlock.Parent = mouse.Target
			game.Workspace.PreviewBlocks:ClearAllChildren()
					local RootPart = player.Character.HumanoidRootPart
					local posit = round(mouse.Hit.p,4)
					local Distance = (posit - Vector3.new(RootPart.Position.X,RootPart.Position.Y,RootPart.Position.Z)).Magnitude
					if Distance <= 35 then
						NewBlock.Transparency = 1
						script.Parent.Activated:Connect(function()
							script.Parent.PartPlaced:FireServer(mouse.TargetSurface, mouse.Target)	
							end)
					end
				
				
			if mouse.TargetSurface == Enum.NormalId.Top then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,4,0)
			elseif mouse.TargetSurface == Enum.NormalId.Left then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(-4,0,0)
			elseif mouse.TargetSurface == Enum.NormalId.Right then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(4,0,0)
			elseif mouse.TargetSurface == Enum.NormalId.Bottom then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,-4,0)
			elseif mouse.TargetSurface == Enum.NormalId.Back then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,0,4)
			elseif mouse.TargetSurface == Enum.NormalId.Front then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,0,-4)
			end	
		end
		end
		end)
		
end

script.Parent.Unequipped:Connect(function()
	game.Workspace.PreviewBlocks:ClearAllChildren()
	player.Character.Blocks:ClearAllChildren()

end)


The entire game lags just by having this tool exist in a players inventory. When I make a seperate tool and remove the distance checking, and move the one that does into serverstorage, the lag goes away completely.

The script is probably very messy because I was trying to scrub at everything to see what was causing the lag. Any help is appreciated.

2 Likes

Try putting task.wait() right below this line and see if that helps.

2 Likes

The lag continues, I thought that would work aswell

2 Likes

The while equipped == true do loop does not have a wait() or task.wait() inside so it runs extremely fast, causing lag.

The loop is not needed in the first place as the mouse.Move event does not need to be connected over and over again to run. Instead you can check if equipped is false inside (or outside) of the function and discontinue the function, if it is false.

(everytime the loop runs, it creates a new function that listens for mouse.Move. The loop itself was bad enough, but creating hundreds of functions on every frame means that hundreds of thousands of functions could run at the same time, if mouse.Move was fired.)

mouse.Move:Connect(function()
    if equipped == false then --if equipped if false, it stops the function. just like that.
       return
    end
  	if player.Character:FindFirstChild("Blocks") then	
		print("foundsuccesful")
		if mouse.Target.ClassName == "Part" then
			print("partfound")

			local NewBlock = script.PreviewPlace:Clone()
			NewBlock.Parent = mouse.Target
			game.Workspace.PreviewBlocks:ClearAllChildren()
					local RootPart = player.Character.HumanoidRootPart
					local posit = round(mouse.Hit.p,4)
					local Distance = (posit - Vector3.new(RootPart.Position.X,RootPart.Position.Y,RootPart.Position.Z)).Magnitude
					if Distance <= 35 then
						NewBlock.Transparency = 1
						script.Parent.Activated:Connect(function()
							script.Parent.PartPlaced:FireServer(mouse.TargetSurface, mouse.Target)	
							end)
					end
				
				
			if mouse.TargetSurface == Enum.NormalId.Top then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,4,0)
			elseif mouse.TargetSurface == Enum.NormalId.Left then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(-4,0,0)
			elseif mouse.TargetSurface == Enum.NormalId.Right then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(4,0,0)
			elseif mouse.TargetSurface == Enum.NormalId.Bottom then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,-4,0)
			elseif mouse.TargetSurface == Enum.NormalId.Back then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,0,4)
			elseif mouse.TargetSurface == Enum.NormalId.Front then
				NewBlock.Parent = workspace.PreviewBlocks
				NewBlock.Position = mouse.Target.Position + Vector3.new(0,0,-4)
			end	
		end
    end
end)

hope this helps :grin:

4 Likes

Thank you! It works. The lag is confined to the tool and not breaking the entire game over it, you saved me because i had no idea what i was doing

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.