How I can improve perfomance of a Radar System?

Hello, I’m working on a radar System that catches everything witch the class ‘Humanoid’ around the player.
Using a loop to filter all humanoids in workspace doesn’t sound a good idea, even because each player have a radar system. I think it can slow game performance significantly. The Minimap shows where exactly are the Humanoids Position, even if the Humanoid moves.
There is a way to improve it?
how the minimap looks like (sorry about the bad quality)


the red dots appear on the minimap when Humanoids are next to the player and disappear when far enough.

and the main script:

function Radar()
	while wait(.5) do
		for i,pilotsOnRadar in pairs(workspace:GetChildren()) do
			if pilotsOnRadar:IsA('Model') and pilotsOnRadar:FindFirstChild('Head') and pilotsOnRadar.Name ~= player.Name then
			
			local target = Instance.new("Part")
			local pos = pilotsOnRadar:GetModelCFrame().p
					
				if player:DistanceFromCharacter(pos) <40  then
                -- I use "alienId" to differentiate them because there are a loot of enemy with the same name
    					if not viewPort.Pilots:FindFirstChild(pilotsOnRadar.Configuration.AlienId.Value) then
    						if pilotsOnRadar:FindFirstChild('Alien') then
    						target.Color = Color3.fromRGB(135,0,0)
    						end	
    						target.Name =  tostring(pilotsOnRadar.Configuration.AlienId.Value)
    						target.Size = Vector3.new(15,0.05,15)
    						target.Anchored = true
    						target.CanCollide = false
    						target.Parent = viewPort.Pilots
    					end
    					viewPort.Pilots:FindFirstChild(pilotsOnRadar.Configuration.AlienId.Value).Transparency = 0
    					target.CFrame = (pilotsOnRadar.HumanoidRootPart.CFrame)
    					print('Radar Catch: '..pilotsOnRadar.Name)
                        --if not in the range
        				elseif viewPort.Pilots:FindFirstChild(pilotsOnRadar.Configuration.AlienId.Value) ~= nil then
        					viewPort.Pilots:FindFirstChild(pilotsOnRadar.Configuration.AlienId.Value).Transparency = 1
        					print(pilotsOnRadar.Name..' Out of Range')
        				end
        			end
        		end
        	end
        end
        spawn(function()Radar()end)

and the script perfomance:
Sem%20t%C3%ADtulo

2 Likes

1.5% isn’t a lot at all. You’re fine.

EDIT: Oh… you’re looping through ALL children in workspace? I think you should tag the things you want to appear on radar with CollectionService instead.

3 Likes

I am interested in learning, and code examples would be really helpful. Do you have any example with that Collection Services? it’s kinda complicated.

1 Like

You’ll basically need to tag the models you want to appear on radar with CollectionService. You can do this vial CollectionService:AddTag(model, "RadarObject") (you can replace "RadarObject" with whatever tag you want, this is just an example). Then, instead of workspace:GetChildren(), you can do CollectionService:GetTagged("RadarObject") to get a list of things that were tagged that. This reduces the number of things you search through by a lot, depending on how many things were under workspace.

3 Likes

truly the better way than workspace.getchildren().

2 Likes

You could use a spatial partitioning system to further filter the tagged objects. Probably not necessary unless you have a lot though.

1 Like