How to prevent script from cloning too many objects in a player

So im trying to make this script not keep cloning a forcefield everytime, how can i only make sure there is one and only one and stop it from cloning again.

while wait() do
		wait(1) 
		if player.BulletHit.Value < 8 then
			player.BulletHit.Value = player.BulletHit.Value + 1
		end
		if player.BulletHit.Value >= 8 then
			repeat
				newshield.Parent = character
			until newshield.Parent == character
			weld.Parent = character
			weld.Part0 = newshield
			weld.Part1 = character.HumanoidRootPart
			forcefield:Clone().Parent = character

There are a few ways you can handle this.
If you want to have only one force field object, you can get the old force field object and call :remove() on its instance.
If you’re trying to do something different than that, you can create a variable in your script like forceFieldAlreadyCloned = false and just set it to true after you clone the force field. Then, you can do this:

if not forceFieldAlreadyCloned then
   forcefield:Clone().Parent = character --Clone the force field.
   forceFieldAlreadyCloned = true --Prevent this block from running again.
end

but how would i use the :remove()
how would i use remove on the ones being cloned over and over again until there is 1

:remove() allows you to delete a part from the Workspace. If the force field part is a child of the Player, then you could use something like oldForceField = player:FindFirstChild("forceField") and either before or after cloning the new force field you can call oldForceField:remove() to delete the part.

Just a side note, but remove() is actually deprecated now

The new way to delete Instances is by using Destroy()

1 Like

I was unaware of this, thank you for informing me

1 Like