Better way to make events happen in sequence

Hello,

As I am making my game, I have a lot of instances where I need events to happen in sequence. To achieve this, I use task.wait(). I don’t really like using this function because I want better structured code, and it’s just bad in general. Here is my current code:

CameraHandler.SetCameraTypeAsync(Enum.CameraType.Scriptable)
CameraHandler.SetCameraPosition(UserHead)
CameraHandler.SetRobloxCore(false)

UserHair.Transparency = 1

task.delay(10, function()
	BlurEffect.Enabled = true
	BlurEffect.Size = 30
	
	AlarmClockSound:Play()
	
	task.wait(7.5)
	
	BlurEffect.Parent = Lighting
	
	BlurAnimation:Play()
	BlinkHandler.StopSleeping()
	
	task.wait(5)
	
	BlurAnimation.Completed:Once(function()
		BlurEffect:Destroy()
	end)
	
	CameraHandler.SetCameraAngleRotation(UserHead, 30.377, 171.692, 57.512)
	CameraHandler.TweenCameraPositionToCameraAngle(UserHead, CameraTweenInfo)
	
	task.wait(5)
	
	CameraHandler.SetCameraAngleRotation(UserHead, 32.404, 29.876, -29.886)
	CameraHandler.TweenCameraPositionToCameraAngle(UserHead, CameraTweenInfo)
	
	task.wait(3.25)
	
	AlarmClockScreen.Text = "6:37 AM"
	
	CameraHandler.SetCameraAngleRotation(UserHead, 31.937, 171.789, 16.717)
	CameraHandler.TweenCameraPositionToCameraAngle(UserHead, CameraTweenInfo)
	
	StopAlarmHint:broadcast(false)
	
	AlarmClockScreen.MouseButton1Down:Once(function()
		GiveBadgeSignal:DisconnectAll()
		AlarmClockSound:Stop()
		StopAlarmHint:cancelBroadcast()

		CameraHandler.SetCameraAngleRotation(UserHead, 32.404, 29.876, -29.886)
		CameraHandler.TweenCameraPositionToCameraAngle(UserHead, CameraTweenInfo)

		task.wait(2.5)

		LampLight.PointLight.Enabled = true
		LampLight.LightSwitch:Play()

		task.wait(5)

		MainBedroom.MainCharacter:Destroy()
		MainBedroom.Bed.Sheets.ComforterTop:Destroy()
		
		local CharacterHumanoidRootPart = EngineClient.Player.Character:WaitForChild("HumanoidRootPart")
		
		CharacterHumanoidRootPart.CFrame = MainBedroom.OutBedSpawnPoint.CFrame
		CameraHandler.SetCameraTypeAsync(Enum.CameraType.Custom)
	end)
	
	GiveBadgeSignal:Fire()
	GiveBadgeSignal:Once(function()
		for index = 1, 60 do
			if index == 60 then
				AlarmClockSound:Stop()
				BadgeAward:broadcast(false)
				StopAlarmHint:cancelBroadcast()
				AwardBadgeNetwork:Fire({2142615135})

				Lighting.ClockTime = 10.5
				workspace:WaitForChild("Terrain"):WaitForChild("Clouds").Cover = 0.6

				CameraHandler.SetCameraAngleRotation(UserHead, 42.54, 60.759, -0.937)
				CameraHandler.TweenCameraPositionToCameraAngle(UserHead, CameraTweenInfo)

				AlarmClockScreen.Text = "9:42 AM"
			end
			task.wait(1)
		end
	end)
end)

while ShouldBlink do
	BlinkHandler.PerformBlink(1)
end

As you can see, I have tons of task.wait()s throughout the script to make camera events happen in sequence. I just want a better way to do this, as said it just makes my code unreadable and messy.

?

local EventsList = {
	[1] = {
		func = function()
			print("do stuff")
		end,
		waittime = 5
	},
	[2] = {
		func = function()
			print("hi")
		end,
		waittime = 5
	}
}

for i = 1, #EventsList do
	EventsList[i].func()
	task.wait(EventsList[i]["waittime"] or nil)
end

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