Making a drop party script

This topic basically evolved from this

Before

So for my game I’m making a drop party module and I want it to drop like 4 things per second.

I thought how I would do it and I came up with this:

for i=1,4 do
    thing:Clone().Parent = workspace
end

or something like that.

Would this work though?

to the whole thing.

Read this reply to get caught up:

After testing I now know that something is wrong because

It won’t drop 4 things per second, it’ll only drop 4 things total. But the code itself seems fine.

1 Like

So what about this then?

while true do
    wait(1)
    for i=1,4 do
        thing:Clone().Parent = workspace
    end
end

No. you’re cloning the script too. so it will run a lot and crash the game

How do you know he’s cloning a script?

1 Like

Assuming “thing” is a part, that would work.

1 Like

you need to put the script outside of the object. when you clone an object it clones with it all of it’s children

1 Like

the script is a child of the object that being cloned. and the clone function clones the object along with it’s children. and since he use script.parent obviously the script is a child of the object

I meant how did you know that the script was in the part. I didn’t see that in the code

He didn’t use script.Parent though

oh i see am wrong. but still we don’t know if the thing variable doesn’t hold the parent

This advice you gave is still true though. @IAmPinleon you need to make sure that the script is in ServerScriptService and it manages the part without the script being in the part (if it is, we don’t know)

2 Likes

Instead of doing it once per second, I’d recommend doing it once per 10-20 seconds and adjusting the amount of parts you spawn accordingly.

Using wait() is generally considered bad practice, especially if you’re waiting small amounts and really often.

Also, you need to pick a random position for the part each time, because by default it’s 0, 0, 0.

1 Like

Ok, here is my function (currently, not finished yet):

local secs = 60
	repeat
		local waitSeconds = math.random(9,20)
		local randomAmount = math.random(4,7)
		wait(waitSeconds)
		for i = 1, randomAmount do
			local clonedCrystal = script.Crystal:Clone()
			clonedCrystal.Parent = workspace.Crystals
			setRandomPosition(clonedCrystal)
		end
		secs = secs - waitSeconds
	until secs <= 0

I’m still using wait() but I want to know how you would do it.

If you don’t mind me asking, is there a better way to wait a small increment of time in what you might call a “good practice” I do agree with using wait() in small increments to be a bad practice but is there any other way to do so in a server script? Sorry if this is random.

RunService.Heartbeat:Wait() or RunService.Stepped:Wait() both work well

2 Likes

If you’re using wait() for waiting larger amounts of time and not as often, you should be good.

1 Like

Ok now if I were to do this let’s say every 30 minutes how would I do this using os.time()?

And on the dot, like 7:00 or 7:30.

And also starting on May 16…

Using wait(n) with n being seconds it is fine. @Zephyr_42 is trying to discuss issues with wait(). But for your situation wait(1) is fine.

You can use os.time(), although i’m not that familiar with it and i prefer tick():

local RunService = game:GetService("RunService")

local cooldown = 20
local lastSpawnedTime = tick()

RunService.Stepped:Connect(function(totalTime, deltaTime) -- these parameters aren't needed but i'm just showing that they exist
    if tick() - lastSpawnedTime >= cooldown then
        spawnThem()
        lastSpawnedTime = tick()
    end
end)