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)