Game gets laggy when shooting laser. Don't know how to optimize

I’m currently making a system where a laser beam shoots at ores if you’re trying to mine them. The problem is that the game gets laggy (6%-23% CPU on task manager, mobile players would struggle) when shooting the laser beams at the ores.

My system basically checks if you’re holding down left click, and if you are it checks if your mouse is on an ore by checking if the “Minable” IntValue exists inside the ore. The game only starts lagging when you’re actually mining an ore, so the lag probably comes from checking the distance and all the if statements, but i’m unsure how i could solve this considering i need to check the distance to see if the laser is supposed to show or not.

The script:

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local laserEnd = game.Workspace.LaserEnd
local laser = game.Workspace.Laser
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.LaserEnd
local hold = false

local function MouseRaycast(blacklist)
	local mousePosition = UIS:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()
	
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	
	return raycastResult
end

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then
		hold = true
	end
end)    

UIS.InputEnded:Connect(function(input)
	local inputType = input.UserInputType
	if inputType == Enum.UserInputType.MouseButton1 then
		hold = false
	end
end)

RunService.RenderStepped:Connect(function()
	if hold == true then
		local result = MouseRaycast({laserEnd})
		if result and result.Instance then
			if result.Instance.Parent:FindFirstChild("Minable") then
				local x = result.Position.X
				local y = result.Position.Y
				local z = result.Position.Z
				
				print(result.Instance)
				
				local cframe = CFrame.new(x,y,z)
				laserEnd.CFrame = cframe
			
				local char = player.Character or player.CharacterAdded:Wait()
				local pos = char:GetPrimaryPartCFrame().p
				local distance = (pos - result.Instance.Parent.PrimaryPart.Position).Magnitude

				if distance <= 30 then
					if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == false then
						laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = true
						laserEnd.ParticleEmitter.Enabled = true
					end
				else
					if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == true then
						laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = false
						laserEnd.ParticleEmitter.Enabled = false
					end
				end
			else
				if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == true then
					laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = false
					laserEnd.ParticleEmitter.Enabled = false
				end
			end
		end
	else
		if laser.LaserBall["Meshes/BallExtra"].Beam.Enabled == true then
			laser.LaserBall["Meshes/BallExtra"].Beam.Enabled = false
			laserEnd.ParticleEmitter.Enabled = false
		end
	end
end)


1 Like

I would first try changing to RunService.Heartbeat RenderStepped should only be used for important camera and character movements/animations, events must be handled before it renders the frame. Also remove your print from the connection, print is a terribly slow function.

Next I would try changing it from updating the laser every frame, to only checking raycasts when the mouse or character moves.

Thanks a lot man, removing the print and changing it to heartbeat made it run smoothly again!

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