While Loop or Render stepped?

I have a localscript that checks if a player has stepped on an Area and if they have a gui pops up as long as no one owns that area. Is this optimal? Or could it be simplified for less potential Lag.

local Players = game:GetService(“Players”)
local RunService = game:GetService(“RunService”)
local Player = Players.LocalPlayer
local Gui = script.Parent
local TouchAreas = workspace:WaitForChild(“TouchAreas”)
local param = OverlapParams.new()
param.FilterType = Enum.RaycastFilterType.Whitelist

----Getting the view and exit view buttons
local AreaViewGui = script.Parent.Parent:WaitForChild(“AreaView”)
local View = AreaViewGui.View

local connection

connection = RunService.RenderStepped:Connect(function()
local Character = Player.Character
local AreaOwned = Player:WaitForChild(“AreaOwned”)

task.wait(.1)

param.FilterDescendantsInstances = {Character.PrimaryPart}
for i, area in pairs (TouchAreas:GetChildren()) do	
	local Overlap = workspace:GetPartsInPart(area, param)
	if #Overlap > 0 then ------ Runs if the area has more than one player
		local ZoneFolder = game.Workspace.Areas:WaitForChild(area.Name)
		local Owner = ZoneFolder:FindFirstChild("Owner")
		
		if AreaOwned.Value == "None" and Owner.Value == "None" then
			Gui[area.Name].Visible = true
		else
			Gui[area.Name].Visible = false
		end
		
		if Owner.Value ~= "None" then ---Checking the players owned area to display the view gui.
			
			local OwnerValueStore = Owner.Value
			
			
		end
		
	else
		
		
			
	end
	
end

end)

2 Likes

A while loop will most likely cause your program to yield as it runs every few seconds, where as RenderStepped runs per or every frame.

The only thing I noticed so far as an error would be to change the RaycastFilterType from Whitelist to Include; just for proper syntax since you’re using overlap params.

(Original post for more context)

1 Like

Just remove this in your RenderStepped connection. In this case, use a RenderStepped connection.

2 Likes