Help on optimizing raycast code

Hello, my script is running up and well but it is very slow and unoptimized.

print("Hello world!")
local ani005335 = game.Workspace:WaitForChild("ani005335")
local player = game.Players.LocalPlayer
local HumanoidRootPart = ani005335:WaitForChild("HumanoidRootPart")
local RunService = game:GetService("RunService")
local part = game.Workspace:WaitForChild("Model")
local lookvector = HumanoidRootPart.CFrame.LookVector
local frame = part.Remorse.PrimaryPart
local pos = HumanoidRootPart.Position + HumanoidRootPart.CFrame.LookVector * 20
local bracket = {}
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteFunction")
local Sphere1 = game.Workspace.Sphere1
local local_player = game.Players.LocalPlayer

local function VisualizeRay(partPos,direction)
	local distance = direction.Magnitude
	local midpoint = partPos.Position + direction / 2
	local newPart = Instance.new("Part")
	newPart.Anchored = true
	newPart.CanCollide = false
	newPart.Material = Enum.Material.Neon
	newPart.Color = Color3.fromRGB(255,0,0)
	newPart.Size = Vector3.new(0.2,0.2,distance+0.1)
	newPart.CFrame = CFrame.new(midpoint, partPos.Position)
	newPart.Parent = workspace
	game.Debris:AddItem(newPart, 0.5)		
end

local function GetDistance()
	local pivot = part:GetPivot()
	local newPivot = CFrame.lookAt(HumanoidRootPart.Position + (HumanoidRootPart.CFrame.LookVector * 30), HumanoidRootPart.Position)
	part:PivotTo(newPivot)
	bracket = {}

	-- Sort visual range children by numeric name
	local visualRangeChildren = part.Remorse["Visual Range"]:GetChildren()
	table.sort(visualRangeChildren, function(a, b)
		return tonumber(a.Name) < tonumber(b.Name)
	end)

	for i, v in ipairs(visualRangeChildren) do
		Sphere1.Position = v.Position - HumanoidRootPart.Position
		local raycastparams1 = RaycastParams.new()
		raycastparams1.FilterType = Enum.RaycastFilterType.Exclude
		raycastparams1.FilterDescendantsInstances = {part, HumanoidRootPart, HumanoidRootPart.Parent.Square, visualRangeChildren}
		local raycast = workspace:Raycast(HumanoidRootPart.Position, v.Position - HumanoidRootPart.Position, raycastparams1)
		--VisualizeRay(HumanoidRootPart, v.Position - HumanoidRootPart.Position, raycastparams1)

		if raycast and (raycast.Instance.Name == "Part") == false then
			print(raycast.Distance)
			print(raycast.Instance)
			table.insert(bracket, raycast.Distance)
		else
			warn("EAT BUGS")
			table.insert(bracket, 0)
		end
	end

	print(bracket)
	return bracket
end

RunService.PreSimulation:Connect(function()
	local finaltable = GetDistance()
	local PlayerGUI = player.PlayerGui
	local Screengui = PlayerGUI:WaitForChild("ScreenGui")
	print(finaltable)

	local Frame = local_player.PlayerGui.ScreenGui.Frame
	local children = Frame:GetChildren()

	-- Sort GUI children by numeric name
	table.sort(children, function(a, b)
		return tonumber(a.Name) < tonumber(b.Name)
	end)

	for _, v in ipairs(children) do
		local index = tonumber(v.Name)
		local value = finaltable[index]

		if value == 0 or value == nil then
			v.BackgroundColor3 = Color3.fromHSV(0,0,0)
		else
			v.BackgroundColor3 = Color3.fromHSV(0, 0, 1 - (value / 25))
		end
	end
end)

Flatland.rbxl (69.1 KB)

nevermind it suddenly became fast

I bet it started being fast once you removed that print(finaltable) and print(bracket). Printing every frame is expensive.

1 Like

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