Do you think doing this is right?

hello everyone, a friend of mine told me "Bro if you repeat your function then you create a lag and latence, its true ???

function GetAllHumanoidRootPart()
	local arrayTable = {}
		for _, basePart in ipairs(workspace:GetDescendants()) do
			if basePart:IsA("BasePart") then
				if basePart.Name == "HumanoidRootPart" and basePart ~= humanoidRootPart then
					table.insert(arrayTable, basePart)
				end
			end
		end
	return arrayTable
end

mouse.Button1Down:Connect(function()
	local aaaa = GetAllHumanoidRootPart()
	print(aaaa)
end)
1 Like

This would likely run smoothly but there is ofc better ways at doing it

1 Like

Why do you need all the humanoid root parts? Another thing, it’s unsafe to check the name, to determine if it’s a root part, instead go through all the players, and if they have a character, get that character model’s primary part instead of checking the name.

Lag and latency are two different things, and you only create lag when you don’t give the CPU time to do other actions like render the actual scene or other scripts, if you run 1000+ lines of code every renderstep, you’re going to get lag but if it’s just a simple loop when you press a button, you should be good.

2 Likes

i just made my own version that would be more effective

(i havent tested it lol)

local players = game:GetService("Players")
local LocalPlayer = players.Localplayer

local char = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumPart = char:WaitForChild("HumanoidRootPart")

function GetAllHumanoidRootPart()
	local arrayTable = {}
	for _, plr in ipairs(players) do
		if plr.Character.HumanoidRootPart ~= HumPart then
			table.insert(arrayTable,plr.Character.HumanoidRootPart)
		end
	end
	return arrayTable
end

mouse.Button1Down:Connect(function()
	local aaaa = GetAllHumanoidRootPart()
	print(aaaa)
end)
2 Likes

thanks for yopu replyyy ! for a player/ npc combat system

1 Like

Here’s some modified code of yours, it implements the system I explained earlier:

local Players = game:GetService("Players")

function GetAllHumanoidRootPart()
	local arrayTable = {}

	for _, player in Players:GetPlayers() do
        if not player.Character or not player.Character.HumanoidRootPart then continue end

		arrayTable[player.Name] = player.Character.HumanoidRootPart -- use dictionary (to know which player is from faster)
	end

	return arrayTable
end

mouse.Button1Down:Connect(function()
	local aaaa = GetAllHumanoidRootPart()
	print(aaaa)
end)

It’s not tested.

1 Like

thanks for your reply and help !!! but I also ennoble the npc

1 Like

try using collection service

char limit : D fgapdfhgjkdfguhsdfuipgh

i dont need use collection service for that

Organize all NPCs into a folder when they’re spawned so you can easily find them by simply iterating over the folder.