Clone() function doesn't loop

  1. What do you want to achieve?
    I’m making a mining system for my game. When the boulder is broken it should drop a random amount of rocks (rock is a tool with a single handle part)
  2. What is the issue?
    The rock only clones once.

the script is inside of the boulder

local replicated = game:GetService("ReplicatedStorage")
local rock = replicated.Rock:Clone()
local rockamount = math.random(2,5)

if health.Value == 0 then
		while rockamount > 0 do
			rock.Parent = workspace
			rock.Handle.Position = boulder.Position
			rockamount -= 1
			print("rock dropped")
		end
		script.Parent:Destroy()
		print("destroyed boulder")
end

So this is what your code is doing:

local replicated = game:GetService("ReplicatedStorage")
local rock = replicated.Rock:Clone() -- Cloning rock once
local rockamount = math.random(2,5)

if health.Value == 0 then
	while rockamount > 0 do
		rock.Parent = workspace -- Parenting the single clone each iteration
		rock.Handle.Position = boulder.Position
		rockamount -= 1
		print("rock dropped")
	end
	script.Parent:Destroy()
	print("destroyed boulder")

What you’ll want to do is something more like this:

local replicated = game:GetService("ReplicatedStorage")
local rock = replicated.Rock

if health.Value == 0 then
	for i=1,math.random(2,5) do
		local newRock = rock:Clone()
		newRock.Parent = workspace
		newRock.Handle.Position = boulder.Position
		newRock = nil
		print("rock dropped")
	end

Make sure to set instance variables to nil when you’re done using them so that they may be garbage collected.

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