Issue with for loop, cloning textbutton multiple times

Hello, my issue is that the for loop in my script clones multiple times the textbutton. I want it to only loop to avoid an infinite amount of textbutton being cloned.


-- Extract of the script.

if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
					local tPlayer = game:GetService("Players"):GetPlayerFromCharacter(Mouse.Target.Parent)
					if tPlayer then
						if tPlayer.Team ~= RedactedTeam then
								local toolschildrens = tPlayer.Backpack:GetChildren()
							
								for i, v in pairs(toolschildrens) do
									if v:IsA("Tool") then
							  	  	    local frame = TextButton:Clone()
							   		    frame.Parent = ItemsFrame
										frame.Text = v.Name
								end
							end
						end
					end
				end

RobloxScreenShot20220827_184938276

I assume this is in some sort of while loop or RunService loop, if so then that’s the reason, before placing the button, make sure it doesn’t exist already

1 Like

How can I make sure it doesn’t exist anymore?

if not object.Parent then
 --Made sure the object doesnt exist! :)
end

Destroyed objects go to nil and nil being used as a condition will always be treated as false

That’s very explaining, thanks.

How can I make the loop run once?

dont use a loop if you want it to run once :slight_smile:

Weird reply, but okay I guess.