RenderStepped error vs While loop

So I have two code blocks, this is the first:

while true do
	local soundSources = CheckZones.CheckZones()

	for _, source in pairs(soundSources) do
		DistanceAttenuation.GetAttenuatedVolume(source)
		EarlyReflectionsRaycast.Raytrace(source)
	end
	wait(0.1)
	for _, part in pairs(game.Workspace.SoundEngine.RaycastParts:GetChildren()) do
		part:Destroy()
	end
end

and this is the following it produces:


now this is the output I want. I am happy with this, however when I try to use the RenderStepped method with this code:

RunService.RenderStepped:Connect(function()
	local soundSources = CheckZones.CheckZones()

	for _, source in pairs(soundSources) do
		DistanceAttenuation.GetAttenuatedVolume(source)
		EarlyReflectionsRaycast.Raytrace(source)
	end
	wait(0.1)
	for _, part in pairs(game.Workspace.SoundEngine.RaycastParts:GetChildren()) do
		part:Destroy()
	end
end)

I get this:


Now why am I complaining if the while loop method works? Well two reasons:

  1. While loops are generally not reccomended
  2. From the first video, you can see if the player moves the raycast initial position can’t keep up, unlike in the renderstepped on.

How do I fix this?
Thanks

I tried using the heartbeat method with Heartbeat:Wait() but that didn’t do anything. anyway to solve this?

I dont see what the problem is? Are you talking about the lasers firing too fast? if so, the reason for it not working is since a function connected to RenderStepped doesnt wait for another function to finish before running. To fix this you could do sometihng like this using a debounce:

local Debounce = false

RunService.RenderStepped:Connect(function()
	if Debounce then return end
	Debounce = true
	local soundSources = CheckZones.CheckZones()

	for _, source in pairs(soundSources) do
		DistanceAttenuation.GetAttenuatedVolume(source)
		EarlyReflectionsRaycast.Raytrace(source)
	end
	wait(0.1)
	for _, part in pairs(game.Workspace.SoundEngine.RaycastParts:GetChildren()) do
		part:Destroy()
	end

	Debounce = false
end)
4 Likes

renderstepped fires every frame BEFORE roblox renders stuff, so it doesnt wait for the lazer to get destroyed before rendering another lazer (since roblox by default runs at 60fps)

so what should I do for this, ive been using a while loop and it works fine BUT its bad practice iirc and also its a bit laggy when the player moves

renderstepped is client sided; it really depends on what you’re trying to do with this

i recon you do what programming_kitten showed and just use a debounce

RenderStepped is flying… toss a task.wait() in there. They say; While loops are generally not recommended, because if you don’t account for everything you may get stuck in an endless loop. Don’t avoid loops, account for not getting stuck in them.

You using RenderStepped for something unrelated to rendering is already scary in itself. Heartbeat (PostSimulation) is the expected default event for any work tied to RunService; the other events are for if you need fine tuning for timing because your code needs to run before physics simulation or rendering, and in the vast majority of cases, you don’t need either.

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