Task.wait() stops entire script

I’m making a quick little title screen for a Iron Lung remake I’m making. But task.wait completely stops the script.
Here is the script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local modulesFolder = replicatedStorage:WaitForChild("Modules")
local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local utilties = require(modulesFolder:WaitForChild("Utilities"))
local createObject = utilties.CreateObject
local tween = utilties.Tween
local titleScreen = {}
function titleScreen:InitState()
	local ambience = Instance.new("Sound", workspace)
	ambience.Name = "Ambience"
	ambience.SoundId = "rbxassetid://5410085763"
	ambience.Looped = true
	if not ambience.Looped then
		repeat task.wait() until ambience.Looped
	end
	ambience:Play()
	local gui = Instance.new("ScreenGui", playerGui)
	gui.Name = "TitleScreen"
	gui.IgnoreGuiInset = true
	local logo = createObject("TextLabel", {
		Parent = gui,
		BackgroundTransparency = 1,
		Size = UDim2.new(1, 0, 0.3, 0),
		Position = UDim2.new(0, 0, 0.25, 0),
		TextScaled = true,
		TextColor3 = Color3.new(1, 1, 1),
		Font = Enum.Font.SpecialElite,
		Text = "IRON LUNG",
		ZIndex = 1
	})	
	local prompt = createObject("TextLabel", {
		Parent = gui,
		BackgroundTransparency = 1,
		Size = UDim2.new(1, 0, 0.15, 0),
		Position = UDim2.new(0, 0, 0.5, 0),
		TextScaled = true,
		TextColor3 = Color3.new(1, 1, 1),
		Font = Enum.Font.SpecialElite,
		Text = "CLICK TO PLAY",
		ZIndex = 1
	})
	local mouse = player:GetMouse()
	mouse.Button1Down:Wait()
	tween(prompt, {TextTransparency = 1}, 2)
	task.wait(1)
	tween(logo, {TextTransparency = 1}, 2)
	task.wait(2)
	logo:Remove()
	prompt:Remove()
	local fader = createObject("Frame", {
		Parent = gui,
		BackgroundColor3 = Color3.new(0, 0, 0),
		BackgroundTransparency = 1,
		Size = UDim2.new(1, 0, 1, 0),
		ZIndex = 2
	})
	tween(fader, {BackgroundTransparency = 0}, 2)
	tween(ambience, {Volume = 0}, 2)
	task.wait(2)
	ambience:Remove()
	local introScript = require(modulesFolder:WaitForChild("IntroScript"))
	introScript:PlayIntro()
	tween(fader, {BackgroundTransparency = 1}, 2)
	task.wait(2)
	gui:Remove()
	script:Remove()
end
return titleScreen

The Button1Down:Wait() stops the script entirely aswell, but when I comment that out the script stops after the tween here:

tween(prompt, {TextTransparency = 1}, 2)
task.wait(1)

Any ideas on how to fix this?

2 Likes

I think instead of task.wait() you should use wait()

1 Like

And use MouseButton1Down:Connect(function())

1 Like

It still stops the entire script.

fasihdfaskjdfasdsafafds

1 Like

hmmmm, im not sure, but this might be a little similar to vizernes idea:

local replicatedStorage = game:GetService("ReplicatedStorage")
local modulesFolder = replicatedStorage:WaitForChild("Modules")
local players = game:GetService("Players")
local player = players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local utilties = require(modulesFolder:WaitForChild("Utilities"))
local createObject = utilties.CreateObject
local tween = utilties.Tween
local titleScreen = {}
function titleScreen:InitState()
	local ambience = Instance.new("Sound", workspace)
	ambience.Name = "Ambience"
	ambience.SoundId = "rbxassetid://5410085763"
	ambience.Looped = true
	if not ambience.Looped then
		repeat task.wait() until ambience.Looped
	end
	ambience:Play()
	local gui = Instance.new("ScreenGui", playerGui)
	gui.Name = "TitleScreen"
	gui.IgnoreGuiInset = true
	local logo = createObject("TextLabel", {
		Parent = gui,
		BackgroundTransparency = 1,
		Size = UDim2.new(1, 0, 0.3, 0),
		Position = UDim2.new(0, 0, 0.25, 0),
		TextScaled = true,
		TextColor3 = Color3.new(1, 1, 1),
		Font = Enum.Font.SpecialElite,
		Text = "IRON LUNG",
		ZIndex = 1
	})	
	local prompt = createObject("TextLabel", {
		Parent = gui,
		BackgroundTransparency = 1,
		Size = UDim2.new(1, 0, 0.15, 0),
		Position = UDim2.new(0, 0, 0.5, 0),
		TextScaled = true,
		TextColor3 = Color3.new(1, 1, 1),
		Font = Enum.Font.SpecialElite,
		Text = "CLICK TO PLAY",
		ZIndex = 1
	})
	local mouse = player:GetMouse()
	mouse.Button1Down:Connect(function()
		tween(prompt, {TextTransparency = 1}, 2)
		task.wait(1)
		tween(logo, {TextTransparency = 1}, 2)
		task.wait(2)
		logo:Remove()
		prompt:Remove()
		local fader = createObject("Frame", {
			Parent = gui,
			BackgroundColor3 = Color3.new(0, 0, 0),
			BackgroundTransparency = 1,
			Size = UDim2.new(1, 0, 1, 0),
			ZIndex = 2
		})
		tween(fader, {BackgroundTransparency = 0}, 2)
		tween(ambience, {Volume = 0}, 2)
		task.wait(2)
		ambience:Remove()
		local introScript = require(modulesFolder:WaitForChild("IntroScript"))
		introScript:PlayIntro()
		tween(fader, {BackgroundTransparency = 1}, 2)
		task.wait(2)
		gui:Remove()
		script:Remove()
	end)
end
return titleScreen
1 Like

There are some bugs in your code (already fixed), that aside, the wait() function will always block your code until the set time elapses or the condition is met, to prevent that from happening you can put the function in task.spawn(). (task.spawn(titleScreen:InitState))

Or use the following code that is already fixed ^-^

local player = game:GetService('Players').LocalPlayer
local mouse = player:GetMouse()
local playerGui = player:WaitForChild('PlayerGui')

function createObject(className, properties, childrens)
	local object = Instance.new(className)
	for i, v in pairs(properties or {}) do
		pcall(function()
			object[i] = v
		end)
	end
	for _, module in pairs(childrens or {}) do
		pcall(function()
			if module:IsA('Instance') then
				module.Parent = object
			end
		end)
	end
	return object
end
function tween(instance, properties, duration, ...)
	local Tween = game:GetService('TweenService'):create(instance, TweenInfo.new(duration, ...), properties)
	Tween:Play()
	return Tween
end

local titleScreen = {}

function titleScreen:InitState()
	createObject('Sound', {
		Parent = workspace,
		Name = 'Ambience',
		SoundId = 'rbxassetid://5410085763',
		Looped = true
	}):Play()

	local gui = createObject('ScreenGui', {
		Parent = playerGui,
		Name = 'TitleScreen',
		IgnoreGuiInset = true
	}, {
		createObject('TextLabel', {
			Name = 'logo',
			BackgroundTransparency = 1,
			Size = UDim2.new(1, 0, 0.3, 0),
			Position = UDim2.new(0, 0, 0.25, 0),
			TextScaled = true,
			TextColor3 = Color3.new(1, 1, 1),
			Font = Enum.Font.SpecialElite,
			Text = 'IRON LUNG',
			ZIndex = 1
		}),
		createObject('TextLabel', {
			Name = 'prompt',
			BackgroundTransparency = 1,
			Size = UDim2.new(1, 0, 0.15, 0),
			Position = UDim2.new(0, 0, 0.5, 0),
			TextScaled = true,
			TextColor3 = Color3.new(1, 1, 1),
			Font = Enum.Font.SpecialElite,
			Text = 'CLICK TO PLAY',
			ZIndex = 1
		}),
		createObject('Frame', {
			Parent = gui,
			Name = 'fader',
			BackgroundColor3 = Color3.new(0, 0, 0),
			BackgroundTransparency = 1,
			Size = UDim2.new(1, 0, 1, 0),
			ZIndex = 2
		})
	})

	mouse.Button1Down:Connect(function()
		tween(gui.prompt, { TextTransparency = 1 }, 2).Completed:Connect(function()
			tween(gui.logo, { TextTransparency = 1 }, 2).Completed:Connect(function()
				tween(gui.fader, { BackgroundTransparency = 0 }, 2)
				tween(gui.ambience, { Volume = 0 }, 2).Completed:Connect(function()
					require(game:GetService('ReplicatedStorage'):WaitForChild('Modules'):WaitForChild('IntroScript')):PlayIntro()
					tween(gui.fader, { BackgroundTransparency = 1 }, 2).Completed:Connect(function()
						gui:Destroy()
					end)
				end)
			end)
		end)
	end)
end
return titleScreen
1 Like

Does your tween function yield? Try removing the task.wait() and tell me if this still happens.

1 Like

task.wait() is likely to be a red herring. Sure feels like these calls to the ‘tween()’ function aren’t returning. Perhaps they are erroring, or internally they are getting into an infinite loop waiting for some condition never happens?

1 Like

show us the utilities script especially the .tween part you dont give us alot to go off when there’s a possibly a custom made tween that could cause issues

1 Like

That is absolutely never a solution to any problem related to yielding, task.wait() is what you should use instead of wait() in all scenarios. The latter is unpredictable and a deprecated function in favour of the task library.

4 Likes

Unfortunately, when I click the mouse it does not work. Everything else seems to work just fine though.

1 Like