Help with despawn time math (seems simple but I am a bit stuck)

So I have a matched based obby and I want the stages to despawn in order randomly. In each match the players have 5 minutes to complete the obby. I also want give the players a 25 second headstart before stages start to despawn. Now the complicated part is I want the last stage to despawn at the end of the match (after 5 minutes). But if stages despawn at a random rate, how would I calculate it so I dynamically despawn each stage randomly but the despawn times add up to where the last stage will end up despawning at the end of the 5 minutes.

This is a little hard to explain lmao but if you put it in a timeline it would look something like this:
Total Stages: 12

25 Second Delay - Head Start
After 12 seconds despawn Stage 1
After 21 Seconds despawn Stage 2
After 18 Seconds despawn Stage 3
…etc

But these random times would have to add up to 4:35 minutes (4:35 minutes and not 5:00 minutes because the 25 second headstart offsets the time)

Also each map will have a different amount of stages, but it should be anywhere from 8-12 stages.

I can’t really test it out but I hope it helps!

local Stages = workspace.Stages -- insert ur stages here
local Minimum = 10
local Maximum = 25
local EndTimer = 300 -- 5 minutes
local CheckTime = 0

for i, stage in pairs(Stages:GetChildren()) do
	local RandomWait = math.random(Minimum, Maximum)
	if CheckTime + RandomWait > EndTimer then
		local Til5 = CheckTime + RandomWait - EndTimer
		local LastWait = EndTimer - Til5
		task.wait(LastWait)
		stage.Enabled = false
		break
	elseif i == MaxStage and not CheckTime + RandomWait > EndTimer then
		local Til5 = CheckTime + RandomWait - EndTimer
		local LastWait = EndTimer - Til5
		task.wait(LastWait)
		stage.Enabled = false
		break
	end
	CheckTime += RandomWait
	task.wait(RandomWait)
	stage.Enabled = false -- assuming it is a spawnpoint
end
1 Like

So it seems like this approach uses random waits, and then once the total time goes over 5 minutes it will lower the values. The hard thing I believe the final wait could be either really short or really long depending on how the numbers roll out. Technically its possible for each stage to despawn after 10 seconds, creating a wait thats like a couple minutes long.

Hmm, then maybe making it so it will always have to take the larger option?

local Stages = workspace.Stages -- insert ur stages here
local MaxStage = #Stages:GetChildren()
local Minimum = 10
local Maximum = 25
local EndTimer = 300 -- 5 minutes
local CheckTime = 0
local Increment = 5
local LastWait

for i, stage in pairs(Stages:GetChildren()) do
	local RandomWait = math.random(Minimum, Maximum)
	if LastWait then
		if LastWait == RandomWait then
			RandomWait += Increment
		end
	end
	if CheckTime + RandomWait > EndTimer then
		local Til5 = CheckTime + RandomWait - EndTimer
		local LastWait = EndTimer - Til5
		task.wait(LastWait)
		stage.Enabled = false
		break
	elseif i == MaxStage and not CheckTime + RandomWait > EndTimer then
		local Til5 = CheckTime + RandomWait - EndTimer
		local LastWait = EndTimer - Til5
		task.wait(LastWait)
		stage.Enabled = false
	end
	CheckTime += RandomWait
	LastWait = RandomWait
	task.wait(RandomWait)
	stage.Enabled = false -- assuming it is a spawnpoint
end

If you want you can check if LastWait is larger than than the Maximum and decrease the time.

1 Like

Alright so this works pretty well although the issue I brought up is still present. It ended up going too fast and the last stage took about 2 minutes to despawn. I will play around with the values though.

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