Can this be done better?

local humanoid = script.Parent.Parent:WaitForChild("Humanoid")

while true do 
	for _, v in workspace:GetChildren() do 
		if not v:FindFirstChild("Humanoid") then 
			continue
		elseif v:FindFirstChild("Humanoid") and v.Name ~= script.Parent.Parent.Name and v:FindFirstChild("HumanoidRootPart") then 
			local distance = (v.HumanoidRootPart.Position - script.Parent.Position).Magnitude
			if distance < 10 then 
				--humanoid:MoveTo(script.Parent.Position + Vector3.new(10, 0, 0)) 
				print("Object near is..."..v.Name)
			else 
				print("Eligible object is EXTREMELY far away and is mid.")
			end 
		end
	end
	task.wait(1)
end

I have some code here which alerts if an object with a humanoid is near the parent, is there a more efficient way to do it?

1 Like

efficient way is restructing whole game
When you load character etc all this caching and such
You can aswell use signals but integrating logic would be better

elseif v:FindFirstChild("Humanoid") is redundant since your if not v:FindFirstChild("Humanoid") already covers it, swtich to else statement and check for v.Name.

also it’d be good if you defined “better”, since it can mean lots of things, such as performance, scalability, etc

1 Like

I kinda only used the else if statement with the humanoid so that I could add the checking for the name & root part

Also I meant since while true’ing with a for loop that covers the entire workspace might cause lag when the script is duplicated, I wanted a performance friendly way that reduces lag

If you are doing this only on players, switching to in next, PlayersService:GetPlayers() will already perform better since you don’t have iterate over whole workspace and don’t have to do Humanoid checks.

additionally if you have NPCs too, you can use CollectionService and tag your players (when they spawn, CharacterAdded event) and also tag your NPCs with some tag like Entity, and only iterate over tagged instances.

local CollectionService = game:GetService("CollectionService")

--// Somewhere in your Server Script
local Players = game:GetService("Players")

local function CharacterAdded(char: Model)
    CollectionService:AddTag(char, "Entity") --// would do this to your NPC model too
end

local function PlayerAdded(plr: Player)
    plr.CharactedAdded:Connect(CharacterAdded) --// tag the Character model, not the Player instance
end

Players.PlayerAdded:Connect(PlayerAdded)
--//

while true do
    for _, Entity in next, CollectionService:GetTagged("Entity")
        local HumanoidRootPart = Entity:FindFirstChild("HumanoidRootPart")
        --// Do your thing
    end
end

Queries might also perform better, haven’t benchmarked yet

1 Like

why do you need to duplicate? (chars)

this is an extremely basic pathfinding script so it needs to be in every enemy character model

it doesn’t, it will be even less performant, one single script handles it all, loops through all NPCs / Enemies, and does the pathfinding, make sure to task.spawn so it doesn’t yield when doing pathfinding

1 Like