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

How can I reduce the laggyness of this script?

mouse.Move:connect(function()
	if player.PlayerGui:FindFirstChild("PickUpItemsNames") ~= nil then
		player.PlayerGui:FindFirstChild("PickUpItemsNames"):Destroy()
	end
	if mouse.Target and mouse.Target.Parent and mouse.Target.Parent.Parent and (mouse.Target.Parent.Parent:FindFirstChild("InvStats") ~= nil or mouse.Target.Parent.Parent:FindFirstChild("StorStats") ~= nil) and (mouse.Target.Position - player.Character.Torso.Position).magnitude <= setdistance then
	if mouse.Target.Parent.Parent:FindFirstChild("InvStats") ~= nil then
			local stats = mouse.Target.Parent.Parent:FindFirstChild("InvStats")
			local screengui = basescreengui:Clone()
			screengui.TextLabel.Text = stats.ItemName.Value
			screengui.TextLabel.Position = UDim2.new(0,mouse.X,0,mouse.Y - screengui.TextLabel.Size.Y.Offset)
			screengui.Parent = player.PlayerGui
			screengui.TextLabel.Visible = true
			if screengui.TextLabel.TextFits == false then
				screengui.TextLabel.Size = UDim2.new(0, screengui.TextLabel.TextBounds.X + 10, 0, screengui.TextLabel.TextBounds.Y)
			end
		else
			local stats = mouse.Target.Parent.Parent:FindFirstChild("StorStats")
			local screengui = basescreengui:Clone()
			screengui.TextLabel.Text = stats.ItemName.Value
			screengui.TextLabel.Position = UDim2.new(0,mouse.X,0,mouse.Y - screengui.TextLabel.Size.Y.Offset)
			screengui.Parent = player.PlayerGui
			screengui.TextLabel.Visible = true
			if screengui.TextLabel.TextFits == false then
				screengui.TextLabel.Size = UDim2.new(0, screengui.TextLabel.TextBounds.X + 10, 0, screengui.TextLabel.TextBounds.Y)
			end
		end
		
	end
	mouse.Button1Down:connect(function()
		if player.PlayerGui:FindFirstChild("PickUpItemsNames") ~= nil then
			player.PlayerGui:FindFirstChild("PickUpItemsNames"):Destroy()
		end
		if mouse.Target and mouse.Target.Parent and mouse.Target.Parent.Parent and mouse.Target.Parent.Parent.ClassName == "Model" and mouse.Target.Parent.Parent:FindFirstChild("InvStats") ~= nil  and (mouse.Target.Position - player.Character.Torso.Position).magnitude <= setdistance then
			local model = mouse.Target.Parent.Parent
		local storageitem = lookthroughstorage(storage:GetChildren(), model.InvStats.Weight.Value)
		if storageitem ~= nil and model.Name == "Primary" then
			model.Parent = toolsquipped.Primary
			model:MoveTo(teleportposition)
			local actualgunbase = gunstorage:FindFirstChild(model.InvStats.ItemName.Value)
			actualgun = actualgunbase:Clone()
			actualgun.Parent = player.Backpack
		elseif storageitem ~= nil then
			storageitem.StorStats.StorageLeft.Value = storageitem.StorStats.StorageLeft.Value - model.InvStats.Weight.Value 
			model.Parent = storageitem.Items
			model:MoveTo(teleportposition)
		end
		elseif mouse.Target and mouse.Target.Parent and mouse.Target.Parent.Parent and mouse.Target.Parent.Parent.ClassName == "Model" and mouse.Target.Parent.Parent:FindFirstChild("StorStats") ~= nil  and (mouse.Target.Position - player.Character.Torso.Position).magnitude <= setdistance then
				local model = mouse.Target.Parent.Parent
			if model.Name == "Backpack" and storage:FindFirstChild("Backpack") == nil then
				local parts = model.Parts
				parts.Name = "Backpack"
				parts.Parent = player.Character
				WeldToTorso(parts.Base, parts.WeldStatsPos.Value, parts.WeldStatsRot.Value)
				model.Items:MoveTo(teleportposition)
				model.Parent = storage
			elseif model.Name == "Belt" and storage:FindFirstChild("Belt") == nil then
				model.Parent = storage
			elseif model.Name == "Shirt" and storage:FindFirstChild("Shirt") == nil then
				model.Parent = storage
				local parts = model.Parts
				parts:MoveTo(teleportposition)
				player.Character.Shirt.ShirtTemplate = model.StorStats.ShirtAsset.Value
			elseif model.Name == "Pants" and storage:FindFirstChild("Pants") == nil then
				model.Parent = storage
				local parts = model.Parts
				parts:MoveTo(teleportposition)
				player.Character.Pants.PantsTemplate = model.StorStats.PantsAsset.Value
			elseif model.Name == "Hat" and storage:FindFirstChild("Hat") == nil then
				model.Parent = storage
			end
		end
	end)
end)

LocalScript that runs under PlayerGui, not full script(missing variables at the top).

I know this is a bit messy, and if cleaning it up will help increase the efficiency, then you'll have to show me how. I was mainly wondering why it is so laggy, and what could I do to fix it? Thanks in advance.

(BTW there are no errors, and I wanted to compress all the picking up items of my storage system into one script.)

-Stats under Script Performance-
Rate			Activity

29/s			0.1%-0.2%		-When mouse is just moving around
285/s- 400/s	5%-30%		-When mouse clicks


--(Put in code block to see it correctly)

Edit 2:If I am missing any needed info put in comments

1 answer

✓ Accepted answer

Cleaning it up doesn't make it more efficient, but it is a pain in the ass to debug without you doing that.

There is no more efficient way, unless you want to get into the hacky stuff, which I don't have a clue on.

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