Why does my script wait 200 seconds instead of 100 before repeating?

this is my script,

local function start()
	if game.Workspace:FindFirstChild("Map") then
		game.Workspace.Map:Destroy()
	end
	local MapClone = game.ReplicatedStorage.Storage.Map:Clone()
	MapClone.Parent = workspace
	wait(2)
	local allPlayers = game.Players:GetChildren()
	for i = 1, #allPlayers do
		allPlayers[i].Team = game.Teams.game
		allPlayers[i].Character.PrimaryPart.CFrame = MapClone.Spawns.Spawn.CFrame
	end
	game.Workspace.Time.Value.Value = 100
	for i = 1,100 do
		wait(1)
		game.Workspace.Time.Value.Value -= 1
		task.wait()
	end
	local allWinners = game.Teams.game:GetChildren()
	for i = 1, #allWinners do
		allWinners[i].leaderstats.Wins.Value += 1
		task.wait()
	end
	wait(0.5)
	local allPlayers = game.Players:GetChildren()
	for i = 1, #allPlayers do
		allPlayers[i].Team = game.Teams.lobby
		allPlayers[i].Character.PrimaryPart.CFrame = game.Workspace.SpawnLocation.CFrame
	end
end
while true do
	start()
	wait(100)
end

for some reason it waits another 100 seconds after the wait to fire start() again

within the start() function, there is a for loop that is telling the Script to wait(1) per 100 iterations

The Loop in Question:

	for i = 1,100 do
		wait(1)
		game.Workspace.Time.Value.Value -= 1
		task.wait()
	end

So yes, it waits 200 instead of 100

2 Likes

Why should that matter as that is only in the function. the problem is it doesn’t fire every 100 seconds

I know, Its Because there is another loop inside the function, I just copy pasted that from the script

2 Likes

yeah but shouldn’t the script be still able to fire the function even when the function has a loop?

no?

That’s not how that works, think of functions as portable and reusable code, it works as normal code but can be used anywhere. It also makes it easier as you don’t have to repeat yourself.

If you want that effect, use coroutine or task.spawn

2 Likes

Just looked it up in documentation and still don’t understand it. What part would I use task.spawn and a coroutine on?

To put it simply, coroutines allow for code to run alongside other code, so if you wanted a loop to run with disrupting the code, you would surround the loop with a coroutine, but if you want to run the code again, you will need to create a new coroutine.

but for the part, you would do:

function Ex()
    task.spawn(function()
        -- code
    end)
end
1 Like

thank you for the help!fdsfdsfdsfs

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