The table produces too many results

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.

local tablet = {}
				for i,v in pairs(workspace:GetDescendants()) do
					if v:FindFirstChild("Humanoid") then
						if (v.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)

This is probably going to add the character for every limb they have. Do a check like this to fix:

if v.Parent:FindFirstChild("Humanoid") then
	if table.find(tablet, v.Parent) then continue end
	if (v.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= 50 then
		table.insert(tablet, v.Parent)
	end
end

Now it’s looks like this

▼ {
[1] = Workspace,
[2] = Workspace,
[3] = Workspace
} - Server - Script:26
20:10:25.461 Workspace - Server - Script:29

You could also try this:

local tablet = {}
for i,v in pairs(workspace:GetDescendants()) do
	if v:IsA("Humanoid") and v.Parent:IsA("Model") then
		if (v.Parent.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= 50 then
			table.insert(tablet, v.Parent)
		end
	end
end

Wow, it’s worked. But I still have a question, is it necessary to add find, it’s just that I’ve never used it before and in general how does it work and what is it for? Thanks

Not in the second code I posted, since theres no chance for duplicates. I suggest using that instead!

The second script work’s too, but v:IsA("Humanoid") it’s prints all Humanoids, but I only need those with the name Humanoid, because I have a Humanoid with the name Zombie for convenience

In any case, I realized that table.find detects whether this object is already in the table, so as not to detect it several times.
(Maybe some words are wrong)

Are you creating the humanoids? If so, why not stick them in a folder named “folder_dummy” and iterate the folders contents with:

for _, child in pairs(folder_dummy:GetChildren()) do
	if (child.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude <= 50 then
		table.insert(tablet, child)
	end
end

This will save an expensive search of the entire workspace (and all of it’s descendants) when the models/parts you require can be partitioned into a separate folder to manage them.

oh it’s because I forgot to remove .Parent in the table.insert

No, in the full script, I’m trying to make sure that when I click the button, I find all the humanoids and player characters, including since they have a humanoid.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.