How to make two scripts run at the same time?

Hi guys,

I wanted to make two scripts run at the same time. I have a script that is going to pick a random tower/obby and another timer script. Those scripts are separated so I added a wait(480) command on the random tower script and the timer is 8 minutes.

However, most of the time the random tower script runs first and so, it will regenerate the tower when the timer is still on 0:12 (or less or more). I used coroutines to make two scripts run at the same time.

However, it doesn’t fix the problem. Here’s the script I’ve used:

Script 1:

local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local minutesvalue = rep:WaitForChild("Minutes")
local secondsvalue = rep:WaitForChild("Seconds")
local minutes = 8 --minutes
local seconds = 0 --seconds

local maps = game.ServerStorage.Maps:GetChildren()
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap")

function chooseMap()
	local choices = {} -- creates a table
	for i = 1, #maps do
		if maps[i]:IsA("Folder") then -- checks if it is a model
			table.insert(choices, maps[i]) -- ads the maps to the table
		end
	end
	local picked = math.random(#maps) -- picks a random map and then sets the chosenmap value to the maps name
	chosenmap.Value = choices[picked].Name
end

function loadMap()
	local map = game.ServerStorage.Maps:FindFirstChild(chosenmap.Value):Clone() -- clones the map determined from the chosenmap value
	map.Parent = currentmap
end

function deleteMap()
	for i,v in pairs(currentmap:GetChildren()) do
		if v:IsA("Folder") then -- checks to see if the things in the currentmap folder is a model
			v:Destroy() -- destroys it if it is a model
		end
	end
end
while true do
	chooseMap()
	loadMap()
	wait(480)
	deleteMap()
end

Script 2:

while true do
	minutesvalue.Value = minutes
	secondsvalue.Value = seconds
	
	repeat
		if secondsvalue.Value <= 0 then
			minutesvalue.Value = minutesvalue.Value - 1
			secondsvalue.Value = 59
		else
			secondsvalue.Value = secondsvalue.Value - 1
		end
		wait(1)
	until secondsvalue.Value <= 0 and minutesvalue.Value <= 0
	
	for _, v in pairs(game.Players:GetPlayers()) do
    v.Character:WaitForChild("BeatObby").Value = false
	
	local allplayers = players:GetPlayers()
		for i, player in pairs(allplayers) do
			wait(0.1)
			player:LoadCharacter()
			wait(0.1)
		end
	end
end

When combined:

local rep = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local minutesvalue = rep:WaitForChild("Minutes")
local secondsvalue = rep:WaitForChild("Seconds")
local minutes = 8 --minutes
local seconds = 0 --seconds

local maps = game.ServerStorage.Maps:GetChildren()
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap")

function chooseMap()
	local choices = {} -- creates a table
	for i = 1, #maps do
		if maps[i]:IsA("Folder") then -- checks if it is a model
			table.insert(choices, maps[i]) -- ads the maps to the table
		end
	end
	local picked = math.random(#maps) -- picks a random map and then sets the chosenmap value to the maps name
	chosenmap.Value = choices[picked].Name
end

function loadMap()
	local map = game.ServerStorage.Maps:FindFirstChild(chosenmap.Value):Clone() -- clones the map determined from the chosenmap value
	map.Parent = currentmap
end

function deleteMap()
	for i,v in pairs(currentmap:GetChildren()) do
		if v:IsA("Folder") then -- checks to see if the things in the currentmap folder is a model
			v:Destroy() -- destroys it if it is a model
		end
	end
end

coroutine.wrap(function()
while true do -- repeats forever and calls the functions
	chooseMap()
	loadMap()
	wait(480)
	deleteMap()
end
end)()

while true do
	minutesvalue.Value = minutes
	secondsvalue.Value = seconds
	
	repeat
		if secondsvalue.Value <= 0 then
			minutesvalue.Value = minutesvalue.Value - 1
			secondsvalue.Value = 59
		else
			secondsvalue.Value = secondsvalue.Value - 1
		end
		wait(1)
	until secondsvalue.Value <= 0 and minutesvalue.Value <= 0
	
	for _, v in pairs(game.Players:GetPlayers()) do
    v.Character:WaitForChild("BeatObby").Value = false
	
	local allplayers = players:GetPlayers()
		for i, player in pairs(allplayers) do
			wait(0.1)
			player:LoadCharacter()
			wait(0.1)
		end
	end
end

Still, the same problem happens. Please let me know if you know any ways to fix this issue. Thanks!

2 Likes

For the amount of time it is doing too fast, you just have to add a wait() the amount of time. I had similar issues and the best way I managed to fix this is by adding wait()

try firing an event and make the timer start when event is fired

Like this? It doesn’t work either.

local players = game:GetService("Players")
local minutesvalue = rep:WaitForChild("Minutes")
local secondsvalue = rep:WaitForChild("Seconds")
local minutes = 8 --minutes
local seconds = 0 --seconds

local maps = game.ServerStorage.Maps:GetChildren()
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap")

function chooseMap()
	local choices = {} -- creates a table
	for i = 1, #maps do
		if maps[i]:IsA("Folder") then -- checks if it is a model
			table.insert(choices, maps[i]) -- ads the maps to the table
		end
	end
	local picked = math.random(#maps) -- picks a random map and then sets the chosenmap value to the maps name
	chosenmap.Value = choices[picked].Name
end

function loadMap()
	local map = game.ServerStorage.Maps:FindFirstChild(chosenmap.Value):Clone() -- clones the map determined from the chosenmap value
	map.Parent = currentmap
end

function deleteMap()
	for i,v in pairs(currentmap:GetChildren()) do
		if v:IsA("Folder") then -- checks to see if the things in the currentmap folder is a model
			v:Destroy() -- destroys it if it is a model
		end
	end
end

coroutine.wrap(function()
while true do
	wait()
	chooseMap()
	loadMap()
	wait(480)
	deleteMap()
end
end)()

while true do
	wait()
	minutesvalue.Value = minutes
	secondsvalue.Value = seconds
	
	repeat
		if secondsvalue.Value <= 0 then
			minutesvalue.Value = minutesvalue.Value - 1
			secondsvalue.Value = 59
		else
			secondsvalue.Value = secondsvalue.Value - 1
		end
		wait(1)
	until secondsvalue.Value <= 0 and minutesvalue.Value <= 0
	
	for _, v in pairs(game.Players:GetPlayers()) do
    v.Character:WaitForChild("BeatObby").Value = false
	
	local allplayers = players:GetPlayers()
		for i, player in pairs(allplayers) do
			wait(0.1)
			player:LoadCharacter()
			wait(0.1)
		end
	end
end

image
Thanks for your reply btw!

I ould strongly recommend RunService.Heartbeat over while wait() do .This would solve both of your problems. Also you should put that into a SINGLE LOOP and have functions handling both of those instead. This way it’s a lot more organized. There’s no reason what so ever to have 2 loops in such a manner up and down from one another.

no, for example, if it runs 8 seconds too fast, you will do wait(8) and then run the rest of the code.

Tested it, but it’s not helping for me. It’s the same.

Thanks for the reply!