Help with using :Wait()

I am currently wanting to fix my main script, my game is based off don’t hit the button and I want the game to wait for the input (clicking the button), it works for the first round, then right after the second intermission, it doesnt detect the clicking anymore.

local Status = game.ReplicatedStorage.Status

function IsOrNotAfkTable(WhichValue)
	local returntab = {}
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character ~= nil then
			if v.Afk.Value == WhichValue then
				table.insert(returntab,v)
			end
		end
	end
	return returntab
end

game.ReplicatedStorage.AfkEvent.OnServerEvent:Connect(function(playerwhosent,isafk)
	print(isafk)
	if isafk == true then
		playerwhosent.Afk.Value = true
	elseif isafk == false then
		playerwhosent.Afk.Value = false
	end
end)


local index = 0

function chooseMap()
	index += 1
	local maps = game.ReplicatedStorage.Stages:GetChildren()
	local Chosenmap = maps[index]
	map = Chosenmap:Clone()
	if index == 21 then
		index = 0
	end
end

local connection

function intermission()

	while (#IsOrNotAfkTable(false)) <= 0 do
		Status.Value = "2 players required to start."
		wait(1)
	end

	for i = 5,0,-1 do
		Status.Value = "Intermission: "..i
		wait(1)
		if i == 1 then break end
	end

	local plrs = IsOrNotAfkTable(false)

	local Spawns = workspace:WaitForChild("MainSpawns"):GetChildren()
	for index, player in pairs(plrs) do
		local RandomSpawn = Spawns[math.random(1, #Spawns)]
		if(player.Character and player.Character.Humanoid and player.Character.Humanoid.Health > 0) then
			player.Character:MoveTo(RandomSpawn.Position) 
		end
	end

	game.Workspace.Door.Bell.ClickDetector.MaxActivationDistance = 10

	Status.Value = "DON'T RING THE DOORBELL!"

	connection = game.Workspace.Door.Bell.ClickDetector.MouseClick:Connect(main) 
end

function main(player)

	connection:Disconnect()

	local plrs = IsOrNotAfkTable(false)

	game.Workspace.Door.Bell.ClickDetector.MaxActivationDistance = 0

	chooseMap()

	workspace.Doorbell:Play()

	Status.Value = player.DisplayName.." has rung the doorbell!"

	wait(2.5)

	map.Parent = workspace
	Status.Value = "Get ready!"

	-- game script takes over

	game.ReplicatedStorage.resetenabler:FireAllClients()

	game.ReplicatedStorage.map.Event:Wait()

	map:Destroy()

	intermission()
end

intermission()
local Status = game.ReplicatedStorage.Status

function IsOrNotAfkTable(WhichValue)
	local returntab = {}
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Character ~= nil then
			if v.Afk.Value == WhichValue then
				table.insert(returntab,v)
			end
		end
	end
	return returntab
end

game.ReplicatedStorage.AfkEvent.OnServerEvent:Connect(function(playerwhosent,isafk)
	print(isafk)
	if isafk == true then
		playerwhosent.Afk.Value = true
	elseif isafk == false then
		playerwhosent.Afk.Value = false
	end
end)


local index = 0

function chooseMap()
	index += 1
	local maps = game.ReplicatedStorage.Stages:GetChildren()
	local Chosenmap = maps[index]
	map = Chosenmap:Clone()
	if index == 21 then
		index = 0
	end
end

local connection

function intermission()

	while (#IsOrNotAfkTable(false)) <= 0 do
		Status.Value = "2 players required to start."
	end

	for i = 5,0,-1 do
		Status.Value = "Intermission: "..i
                wait(0.1)
		if i == 1 then break end
	end

	local plrs = IsOrNotAfkTable(false)

	local Spawns = workspace:WaitForChild("MainSpawns"):GetChildren()
	for index, player in pairs(plrs) do
		local RandomSpawn = Spawns[math.random(1, #Spawns)]
		if(player.Character and player.Character.Humanoid and player.Character.Humanoid.Health > 0) then
			player.Character:MoveTo(RandomSpawn.Position) 
		end
	end

	game.Workspace.Door.Bell.ClickDetector.MaxActivationDistance = 10

	Status.Value = "DON'T RING THE DOORBELL!"

	connection = game.Workspace.Door.Bell.ClickDetector.MouseClick:Connect(main) 
end

function main(player)

	connection:Disconnect()

	local plrs = IsOrNotAfkTable(false)

	game.Workspace.Door.Bell.ClickDetector.MaxActivationDistance = 0

	chooseMap()

	workspace.Doorbell:Play()

	Status.Value = player.DisplayName.." has rung the doorbell!"

	map.Parent = workspace
	Status.Value = "Get ready!"

	-- game script takes over

	game.ReplicatedStorage.resetenabler:FireAllClients()

	game.ReplicatedStorage.map.Event:Wait()

	map:Destroy()

	intermission()
end

intermission()

Try this, tested it on studio and works for me.

What exactly did you change in the script? @Yxnviction

1 Like

Removed some wait() which is not needed to be as I guess, since it’s works for me it’s fine now.

Why is that wait unnecessary, it lets my status’s text be shown… for 2.5 seconds

Then: ```
map.Parent = workspace
wait(2.5)
Status.Value = “Get ready!”

Status.Value = player.DisplayName.." has rung the doorbell!"

	map.Parent = workspace
	Status.Value = "Get ready!"

This would just skip over, not saying who rung it

Yeah I got it now and already fixed, so you should use wait() after map.Parent = workspace, for ex: map.Parent = workspace
wait(2.5)
Status.Value = ‘Get Ready’

Using proximity prompts instead, they are better than use mouseclick