Hey DevForum!
I wrote a very simple light Bounce simulator with ray casting, but I just can’t get it to run smoothly even when I tried implementing Parallel Luau. Am I doing something wrong or is it not possible to make this perform better.
Code:
(Parented it in under an Actor in StarterPlayer and parent a boolean value called debugEnabledValue to it)
local debrisService = game:GetService("Debris")
local runService = game:GetService("RunService")
local debounce = false
local targetBrightness
local targetCFrame
local targetColor
local targetPosition
local targetRange
local times = 8
local intensity = 0.5
local updateTime = 5
local renderDistance = 50
local function gatherResults(lightPart, raycastParams)
local results = {}
for angle = 0, 360, 360/times do
for angle2 = 0, 360, 360/times do
--
local x1 = math.cos(math.rad(angle))
local x2 = math.sin(math.rad(angle2))
local y1 = math.sin(math.rad(angle))
local y2 = math.sin(math.rad(angle2))
local z = math.cos(math.rad(angle2))
local x = x1 * x2
local y = y1 * y2
--
local origin = lightPart.Position
local direction = Vector3.new(x,y,z)
--
local raycastResult = workspace:Raycast(origin, direction.Unit*5000)
--
if raycastResult then
if script.debugEnabledValue.Value == true then
--[[
local distance = (origin - raycastResult.Position).Magnitude
local raypart = Instance.new("Part", workspace.ignoreFolder)
raypart.Anchored = true
raypart.CanCollide = false
raypart.CastShadow = false
raypart.CanQuery = false
raypart.Position = raycastResult.Position
raypart.CFrame = CFrame.lookAt(raycastResult.Position, origin) *CFrame.new(0, 0, -distance/2)
raypart.Material = Enum.Material.Neon
raypart.Name = tostring(angle).." : "..tostring(angle2)
raypart.Size = Vector3.new(0.05,0.05,distance)
--]]
end
table.insert(results, raycastResult)
end
end
end
return results
end
runService.RenderStepped:Connect(function()
if debounce == true then return end
debounce = true
for _, light in pairs(workspace:GetDescendants()) do
if light:IsA("PointLight") then
if not light:IsA("PointLight") or light:FindFirstAncestor(workspace:WaitForChild("ignoreFolder").Name) then continue end
if not light:FindFirstAncestorWhichIsA("Part") or light.Enabled == false then continue end
local lightPart = light:FindFirstAncestorWhichIsA("Part")
--
local distance = (workspace.CurrentCamera.CFrame.Position - lightPart.Position).Magnitude
if distance > renderDistance then continue end
--
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {lightPart}
--
task.desynchronize()
local results = gatherResults(lightPart, raycastParams)
if #results == 0 then continue end
task.synchronize()
for _, result:RaycastResult in pairs(results) do
targetBrightness = intensity/ result.Distance*2
targetCFrame = CFrame.lookAt(result.Position, lightPart.Position)
if result.Instance:IsA("BasePart") then
targetColor = result.Instance.Color
else
targetColor = light.Color
end
targetPosition = result.Position
targetRange = result.Distance
local bouncePart = Instance.new("Part", workspace.ignoreFolder)
bouncePart.Anchored = true
bouncePart.Position = targetPosition
bouncePart.CFrame = targetCFrame
bouncePart.CanCollide = false
bouncePart.CanQuery = false
bouncePart.CanTouch = false
bouncePart.Size = Vector3.new(1,1,1)
if script.debugEnabledValue == true then
bouncePart.Transparency = 0
local decal = Instance.new("Decal")
decal.Face = Enum.NormalId.Front
decal.Texture = "rbxassetid://106411906905300"
decal.Parent = bouncePart
else
bouncePart.Transparency = 1
end
local bounceSpotLight = Instance.new("SpotLight", bouncePart)
bounceSpotLight.Brightness = targetBrightness
bounceSpotLight.Color = targetColor
bounceSpotLight.Range = targetRange
debrisService:AddItem(bouncePart, updateTime)
end
end
end
task.wait(updateTime)
debounce = false
end)
Any help is very much appreciated and I hope you have a nice day or evening!
Hierarchy

