Script is lagging really badly!

Why is this script cause an immense amount of lag?

The local script is located in StarterCharacterScripts

function InterfaceCheck()
	for _,v in pairs(workspace:GetDescendants()) do
		if v:IsA("Seat") then
			local Mag = (v.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
			
			if Mag <= CarRange and v.Occupant == nil and not Humanoid.Sit then
				local NewInterface = Interface:Clone()
				NewInterface.InputText.Text = "Sit"
				NewInterface.Parent = v
				if Mobile then
					NewInterface.BG.Key.Text = "TAP"
				end
			else
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
					v.Disabled = true
				end
			end
			
			if Humanoid.Sit then
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
					v.Disabled = true
				end
			end
			
			if v.Occupant ~= nil then
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
					v.Disabled = true
				end
			end
		end
		
		if v:IsA("VehicleSeat") then
			local Mag = (v.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude

			if Mag <= CarRange and v.Occupant == nil and not Humanoid.Sit then
				local NewInterface = Interface:Clone()
				NewInterface.InputText.Text = "Driver Sit"
				NewInterface.Parent = v
				if Mobile then
					NewInterface.BG.Key.Text = "TAP"
				end
			else
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
					v.Disabled = true
				end
			end
			
			if Humanoid.Sit then
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
					v.Disabled = true
				end
			end
			
			if v.Occupant ~= nil then
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
					v.Disabled = true
				end
			end
		end
		
		if v:IsA("Part") and v.Name == "CarPad" then
			local Mag = (v.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude

			if Mag <= CarRange then
				local NewInterface = Interface:Clone()
				NewInterface.InputText.Text = "Spawn Car"
				NewInterface.Parent = v
				if Mobile then
					NewInterface.BG.Key.Text = "TAP"
				end
			elseif Mag > CarRange then
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
				end
			end
		end
		
		if v:IsA("Part") and v.Name == "BankOpen" then
			local Mag = (v.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude

			if Mag <= CarRange then
				local NewInterface = Interface:Clone()
				NewInterface.InputText.Text = "Withdraw / Deposit"
				NewInterface.Parent = v
				if Mobile then
					NewInterface.BG.Key.Text = "TAP"
				end
			elseif Mag > CarRange then
				if v:FindFirstChild("Interface") then
					v.Interface:Destroy()
				end
			end
		end
	end
end

spawn(function()
	while true do
		InterfaceCheck()
		wait(0.1)
	end
end)

It is not a good idea to use :GetDescendants() every 0.1 seconds. I’ve experienced the same problem though and decided to use something else instead. I suggested you run :GetDescendants() one time and get all of the seat then put it in a single table and get all of the seat within the table instead.

3 Likes