Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make this script spawn 20 rocks, and then spawn rocks back to 20 when rocks destroyed?

So, ive got this script that is supposed to spawn 20 rocks inside the given area, and then only spawn more when there are less than 20, all the way back up to 20. i get no errors, but it doesnt spawn anything. a

local function GenerateRocks()
	for i = 1, 20 do
		local numberofrocks = workspace.Rocks:GetChildren()
		local RockClone = game.ReplicatedStorage.Rock1:Clone()
		if #numberofrocks == 20 then
			break
		
		else
			RockClone.Parent = workspace.Rocks

			local RandomX = math.random(-86.341, -71.519)
			local RandomZ = math.random(136.349, 143.938)
	
			RockClone.Position = Vector3.new(RandomX, 2, RandomZ)
		end
	end	
end
GenerateRocks()

1 answer

✓ Accepted answer

First of all, you should remove all unused custom arguments in your function, meaning there should be nothing in the parentheses in the first line.

Also, you have to actually activate the function like so:

GenerateRocks()

27 comments

0

This still doesnt resolve my issue. None of the rocks are spawning

0

Are you sure the rocks are not spawning, and they aren't just falling into the void?

0

I am sure that they aren't falling into the void

0

Well maybe try posting a video so others can better understand the problem and try to fix it

0

Make sure the positions are set correctly and show the revised version of the script

0

Just updated it, and the positions are correct

0

Put a GenerateRocks() on line 12

0

It's kind of working, but they keep spawning in the same place now

0

Are you getting an error in the output that says "invalid argument #2 to 'random' (interval is empty)"? If so, you need to reverse the order of the arguments in your RandomZ to math.random(136.349, 143.938)

0

If that works, be sure to mark the post as a solution!

0

Sorry, internet cut out. That didnt fix it,

0

No errors though

0

Well it worked for me I don't know why it wouldn't work for you, did you change line 7 to local RandomZ = math.random(136.349, 143.938)?

0

Yes. They are spawning, but only in one place, every single time, and more dont spawn when I destroy them

0

Well you haven't implemented the feature to spawn more when there are less than 20 yet

0

Oh. I see.

0

I've just updated the script

0

Make sure the rock in ReplicatedStorage does not have a weld, and to make it spawn more when rocks are destroyed you must check if a rock has been destroyed and fire GenerateRocks() again.

0

Wouldn't that just spawn 20 more rocks?

0

And also, I've made sure there are no welds, but it is a union

0

No, it wouldn't spawn 20 more rocks because you checked for the numberofrocks to not be 20 in order to spawn more rocks.

0

But I still don't know why they are all spawning in the same spot if there is only one Union in ReplicatedStorage called Rock1 with no welds and the script is in ServerScriptService, because it works for me

0

Well, im not really sure what to do at this point. Could you possibly link a video or image of what your workspace looks like?

0

I actually tried it on a test coding map I had, but I don't think the other things would affect the rock spawning thing. I boxed all of the things that matter: https://cdn.discordapp.com/attachments/980702393824055340/980703108453781545/unknown.png

0

This is the script that I use, and every 10 seconds the amount of "Rock1" in the folder "Rocks" is 20 https://cdn.discordapp.com/attachments/780140523088379906/980703736970244096/unknown.png

0

I cant view any of the images.

0

It works. I just had a really bad typo, Thanks for your help

This discussion is preserved as it was. New answers, comments, and votes are closed.