In pairs loop run repeatedly on Instance.new

I was using Instance.new(“Highlight”) and it instance alot of it on one object

Hello there,

Please attach your code or an appropriate segment of your code containing said problem. We have absolutely nothing to base our solutions off with such little detail provided.

					for i,v in pairs(workspace:GetDescendants()) do
						local Character = v:FindFirstAncestorWhichIsA('Model')
						local Humanoid = Character:FindFirstChildWhichIsA('Humanoid')
						if Humanoid and Humanoid.Parent.Name ~= character.Name then
							local highlight = Instance.new("Highlight")
							highlight.FillTransparency = 1
							highlight.Parent = Humanoid.Parent
						end
					end

This is happening since the GetDescendants() array contains multiple objects that are part of the same Character, and hence your code is executed on each of them, despite the descendant still being a part of the same Character model.

To fix this, simply add a condition that checks if the Character already has a Highlight parented to it:

for i,v in pairs(workspace:GetDescendants()) do
	local Character = v:FindFirstAncestorWhichIsA('Model')
	local Humanoid = Character:FindFirstChildWhichIsA('Humanoid')
	
	if Humanoid and Humanoid.Parent.Name ~= character.Name and not Humanoid.Parent:FindFirstChildOfClass("Highlight") then
		local highlight = Instance.new("Highlight")
		highlight.FillTransparency = 1
		highlight.Parent = Humanoid.Parent
	end
end

THANKS i forgor about checking children

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