Hello! I created a script that should detect humanoids and add them to the table.Everything works, but there are only 4 humanoids in my workspace, and the table shows 51. I reviewed and realized that the humanoid that should be in table 1 is shown several times, after which the next humanoid is shown again several times and so on.
No one has encountered such a problem on the forum, at least I have not found such posts.
Script:
local tablet = {}
for i,v in pairs(workspace:GetDescendants()) do
if v.Parent:FindFirstChild("Humanoid") then
if (v.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= 50 then
table.insert(tablet, v.Parent)
end
end
end
print(tablet)
local random = math.random(1,#tablet)
local randomplayer = tablet[random]
print(randomplayer)
Output:
▼ {
[1] = Dummy,
[2] = Dummy,
[3] = Dummy,
[4] = Dummy,
[5] = Dummy,
[6] = Dummy,
[7] = Dummy,
[8] = Dummy,
[9] = Dummy,
[10] = Dummy,
[11] = Dummy,
[12] = Dummy,
[13] = Dummy,
[14] = Dummy,
[15] = Dummy,
[16] = Dummy,
[17] = Dummy,
[18] = Dummy,
[19] = Dummy,
[20] = Dummy,
[21] = Dummy,
[22] = Dummy,
[23] = Dummy,
[24] = Dummy,
[25] = Dummy,
[26] = Dummy,
[27] = Dummy,
[28] = Dummy,
[29] = Dummy,
[30] = Dummy,
[31] = Dummy,
[32] = Dummy,
[33] = Dummy,
[34] = Dummy,
[35] = Dummy,
[36] = Dummy,
[37] = Dummy,
[38] = Dummy,
[39] = Dummy,
[40] = Dummy,
[41] = Dummy,
[42] = Dummy,
[43] = Dummy,
[44] = Dummy,
[45] = Dummy,
[46] = Dummy,
[47] = Dummy,
[48] = Dummy,
[49] = Dummy,
[50] = Dummy,
[51] = Dummy
}
I could leave everything like this, because the script performs the main task, but that would be wrong on my part and probably created a lot of load.