Help with making my helicopter script run after a certain amount of time

I am making a game where you have to survive until the escape helicopter arrives to pick you up but I have a script that runs the Intermission period and the game period but after the game starts I want it to “Activate” a script that teleports the escape helicopter to certain positions I here are the scripts:

local Map = game.ServerStorage.Map.Map
local ingame = game.Workspace.Map
local status = game.ReplicatedStorage.Values.RoundCounter
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local timeEvent = ReplicatedStorage.Events:WaitForChild('TimeEvent')

local function playGame()
	local timeAmount = 300
	local seconds = ' Seconds!'
	local timerText = 'Escape Helicoper Arriving In: '
	
	while timeAmount > 0 do
		timeEvent:FireAllClients(timeAmount, timerText, seconds)
		wait(1)
		timeAmount -= 1
	end
end

local function playIntermission()
	local intermission = 138
	local seconds = ' Seconds!'
	local timerText = 'Game Starting In: '
	while intermission > 0 do
		timeEvent:FireAllClients(intermission, timerText, seconds)
		wait(1)
		intermission -= 1
	end
end


while true do
	playIntermission()
	playGame()
end

This script basically runs the game function and the intermission function on a while loop but inside the “playGame” function I want it to wait until the time remaining is 90 seconds then maybe fire an event that triggers my teleport helicopter script and here is my Teleport helicopter script:

local status = game.ReplicatedStorage.Values.IsInRound
local status2 = game.ReplicatedStorage.Values.CameraChanger
local Event = game.ReplicatedStorage.Events.HeliMover
local Sound = game.Workspace.Helicopter
local ingame = game.ServerStorage.Heli.Helecopter:Clone()


if ingame.Parent == game.Workspace then
	local Heli= game.Workspace.Helecopter
	
	ingame.Parent = game.Workspace
	ingame:MoveTo(Vector3.new(276.5, 71.5, -786))
	wait(2)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -808)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -681.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -591.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -489)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -396)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -286.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 78.5, -167.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 43, -167.5)))
	wait(45)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, -167.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, -77.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, -77.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, -38)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 93.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 93.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 163.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 231)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 313.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 392.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 496.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 496.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 690)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 797.5)))
	wait(1.3)
	ingame:SetPrimaryPartCFrame(CFrame.new(Vector3.new(265, 100.5, 881.5)))
	status2.Value = true
	print("COMPLETED HELICOPTER FLIGHT!")
	if game.Workspace.Helicopter.IsPlaying then
		Sound:Pause()
		print("Sound Paused!")
	end
	wait(20)
	ingame:Destroy()
end)

This script looks messy but all it does is teleport the helicopter to the positions, Sorry if this question is confusing thanks.

You can disabled and enable scripts to make it run at a certain time. You just need to define the name of the script.

helicopterscript.Disabled = true
helicopterscript.Disabled = false
1 Like

First of all, for all these sections you can get rid of them by simply tweening. Can’t currently explain it but you can look it up on the dev hub for more info!

1 Like

Why not just check if timeAmount == 90 and then fire a function?

1 Like
if timeAmount > 90 then
		game.ServerScriptService.HeliMover.Disabled = false
		wait(80)
		game.ServerScriptService.HeliMover.Disabled = true
	end

This works but if I put this before it fires to all the clients it breaks the countdown but if I put it after it does not work at all what should I do?

You can put the section of code in a coroutine

coroutine.wrap(function()
  if timeAmount > 90 then -- Code in here wont yield the code outside
		game.ServerScriptService.HeliMover.Disabled = false
		wait(80)
		game.ServerScriptService.HeliMover.Disabled = true
	end
end)()
1 Like

Where should I put this code it does not seem to work, Thanks.

ohhh nvm I just for got to add an “end” thank you so much