Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 1 year ago
Edited 1 year ago

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

Log in to vote
0
Answered by
boredlake 256 Moderation Voter
1 year ago

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()
0
This still doesnt resolve my issue. None of the rocks are spawning NightminerYT 2 — 1y
0
Are you sure the rocks are not spawning, and they aren't just falling into the void? boredlake 256 — 1y
0
I am sure that they aren't falling into the void NightminerYT 2 — 1y
0
Well maybe try posting a video so others can better understand the problem and try to fix it boredlake 256 — 1y
View all comments (23 more)
0
Make sure the positions are set correctly and show the revised version of the script boredlake 256 — 1y
0
Just updated it, and the positions are correct NightminerYT 2 — 1y
0
Put a GenerateRocks() on line 12 boredlake 256 — 1y
0
It's kind of working, but they keep spawning in the same place now NightminerYT 2 — 1y
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) boredlake 256 — 1y
0
If that works, be sure to mark the post as a solution! boredlake 256 — 1y
0
Sorry, internet cut out. That didnt fix it, NightminerYT 2 — 1y
0
No errors though NightminerYT 2 — 1y
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)? boredlake 256 — 1y
0
Yes. They are spawning, but only in one place, every single time, and more dont spawn when I destroy them NightminerYT 2 — 1y
0
Well you haven't implemented the feature to spawn more when there are less than 20 yet boredlake 256 — 1y
0
Oh. I see. NightminerYT 2 — 1y
0
I've just updated the script NightminerYT 2 — 1y
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. boredlake 256 — 1y
0
Wouldn't that just spawn 20 more rocks? NightminerYT 2 — 1y
0
And also, I've made sure there are no welds, but it is a union NightminerYT 2 — 1y
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. boredlake 256 — 1y
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 boredlake 256 — 1y
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? NightminerYT 2 — 1y
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 boredlake 256 — 1y
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 boredlake 256 — 1y
0
I cant view any of the images. NightminerYT 2 — 1y
0
It works. I just had a really bad typo, Thanks for your help NightminerYT 2 — 1y
Ad

Answer this question