Issues with i,v in pairs loop stopping studio

I’m working on a minigame for one of my games, and its one of those games where you sit on a tile and weird stuff happens. I’m currently making the system where it checks what round it is, how much time until the next event, and what the event is. My issue right now is one of my i,v pairs is stopping studio and subsequently the entire script.

The issue:

while started do
	--lower clock
	for count = timeleft, 0, 1 do
		timeleft = timeleft - 1
		print(timeleft)
	end
	
	if timeleft == 5 then
		local random = math.random(1,2)
		for i,v in pairs(events) do --right here
			if i == random then
				
				--update UI 5 seconds before event
				for i,p in pairs(game.Players:GetChildren()) do
					p.PlayerGui.ScreenGui.UI.Time.Event = ("You have 5 seconds before a ".. v)
				end
				wait(5)
				

				v()
				round = round + 1
				for i,v in pairs(game.Players:GetChildren()) do
					v.PlayerGui.ScreenGui.UI.Round.Text = ("Round "..round)
				end

			end
		end
	end
end

The script: (long)

local plates = script.Parent.plates
local started = false
local assets = script.Parent.eventassets
local UI = script.Parent.UI

local timeleft = 30
local round = 1

started = true

--tp players to their plates & give them their UI
if started == true then
	for i,v in pairs(game.Players:GetChildren()) do
		print(v.Name)
		local char = v.Character
		char:MoveTo(plates:FindFirstChild(i).Position + Vector3.new(0,5,0))
		
		--give them their UI
		local clone = UI:Clone()
		clone.Parent = v.PlayerGui.ScreenGui
	end
end

--event functions

--random plate becomes disco
function Disco()
	local random = math.random(1,16)
	for i,v in pairs(plates:GetChildren()) do
		if v:FindFirstChild("disco") then
			--nothing
		elseif i == random then
			print("Playing event")
			v.Material = "Foil"
			local disco = script.disco:Clone()
			disco.Parent = v
			disco.Disabled = false
		end
	end 
end

--meteor
function Meteor()
	local meteor = assets.Meteor
	local random = math.random(1,16)
	for i,v in pairs(plates:GetChildren()) do
		if i == random then
			print("Playing event")
			local meteor = meteor:Clone()
			meteor.Parent = game.Workspace
			meteor.Anchored = false
			meteor.Position = v.Position + Vector3.new(0,300,0)
			wait(5)
			meteor:Destroy()
		end
	end 
end

--random events

local events = {Disco, Meteor}

while started do
	--lower clock
	for count = timeleft, 0, 1 do
		timeleft = timeleft - 1
		print(timeleft)
	end
	
	if timeleft == 5 then
		local random = math.random(1,2)
		for i,v in pairs(events) do
			if i == random then
				
				--update UI 5 seconds before event
				for i,p in pairs(game.Players:GetChildren()) do
					p.PlayerGui.ScreenGui.UI.Time.Event = ("You have 5 seconds before a ".. v)
				end
				wait(5)
				

				v()
				round = round + 1
				for i,v in pairs(game.Players:GetChildren()) do
					v.PlayerGui.ScreenGui.UI.Round.Text = ("Round "..round)
				end

			end
		end
	end
end
1 Like

Update: I just found an issue, im counting from the count value, to 0 by increments of 1, when in reality i should be counting by 30, to 0, by increments of 1, but I still get the same issue at the same spot.

Have you tried placing a wait(n) inside the while loop? From the looks of it, there’s a wait(5) in the conditional statement but not the while loop body?

2 Likes