Cloning my object makes more then 1 spawn

Hey, just needing some help with my script. Whenever I clone the ball it just spawns in like 5 at once. What have I done wrong?

while true do
	wait(5)
local clonedObject = ball:Clone()
clonedObject.Parent = workspace
clonedObject.Position = Vector3.new(5.227, 22.6, 176.391)
wait(0.1)
ball:Destroy()
end
1 Like

You are destroying the original ball. Not the clone. You need to use clonedObject:Destroy(). Not ball:Destroy().

1 Like

while wait(5) do

if game.Workspace:FindFirstChild(“ball”) == nil then

local clonedObject = ball:Clone()
clonedObject.Parent = workspace
clonedObject.Position = Vector3.new(5.227, 22.6, 176.391)
wait(0.1)
ball:Destroy()
end
end

Try this.

1 Like

i suggest adding a Debounce to your script.

local debounce = false
while true do
    if debounce == false then
        debounce = true
        local clonedObject = ball:Clone()
        clonedObject.Parent = workspace
        clonedObject.Position = Vector3.new(5.227, 22.6, 176.391)
        wait(0.1)
        ball:Destroy()
        debounce = false
2 Likes

Ok, I have tried this so far:

while true do
	wait(3)
local clonedObject = ball:Clone()
clonedObject.Parent = workspace
clonedObject.Position = Vector3.new(13.227, 22.6, 176.391)
if clonedObject >=2 then
	ball:Destroy()
else
wait(3)
clonedObject:Destroy()
end
end

While this does seem to work, an error in the output is mentioned everytime the ball is cloned.

  08:54:29.138 - Workspace.Ball.Script:18: attempt to compare number and userdata
08:54:29.139 - Stack Begin
08:54:29.140 - Script 'Workspace.Ball.Script', Line 18
08:54:29.140 - Stack End

So what are you comparing on line 18 of the Workspace.Ball.Script that would throw that error?

this is what is making the error.

something like this will work:

for i,v in pairs(workspace:GetChildren()) do
  if v == clonedObject then
      if i == 2 then
         ball:destroy()
      end
   end
end

I may be wrong on the i==2.

Just use a debounce.

Quoting your original code, with edits:

local dB
while true do
wait(5)
if not dB then
dB = true
local clonedObject = ball:Clone()
clonedObject.Parent = workspace
clonedObject.Position = Vector3.new(5.227, 22.6, 176.391)
wait(0.1)
ball:Destroy()
dB = false
end
end

If you’re wondering why I didn’t set dB to anything to start with, nil is falsey and that’s what declared but not initialized variables are set to.

Nothing works. I am still trying to figure out what is happening

OK, I did more testing and, if you only want one ball at a time. You need to do something like this instead of what your doing:

while true do
local clonedObject = ball:Clone()
clonedObject.Parent = workspace
clonedObject.Position = Vector3.new(5.227, 22.6, 176.391)
wait(0.1)
ball:Destroy()
wait(5)
clonedObject:Destroy()
end

This will destroy the previous ball before making a new one. Right now, you never destroy any of the balls. I don’t know if that is what you are trying to achieve but, I hope this helped.

1 Like