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)
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.
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)
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)