Spectate Freezes game

My spectate script freezes the game, i’ve been informed it might be the repeat line but I don’t know what to replace it with.

I’ve tried doing research but I’m still confused

alive = {}
watch = 1

function getAlive()
	local t = {}
	for _,p in ipairs(game.Players:GetChildren()) do
		if p.Character~=nil and p.Character:FindFirstChild("Humanoid")~=nil and p.Character.Humanoid:FindFirstChild("CharName")~=nil then
			table.insert(t,p)
		end
	end
	table.sort(t,function(a,b)
		local char = 0
		repeat 
			char = char+1
		until string.sub(a.Name,char,char)~=string.sub(b.Name,char,char)
		return string.byte(string.sub(a.Name,char,char))<string.byte(string.sub(b.Name,char,char))
	end)
	return t
end

function startSpectate()
	if not spectating then
	alive = getAlive()
	if #alive>0 then
	spectating = true
	watch = 1
	workspace.CurrentCamera.CameraSubject = alive[1].Character.Humanoid
	script.Parent.Parent.Center.Visible = true
	script.Parent.Parent.ClickRight.Visible = true
	script.Parent.Parent.ClickLeft.Visible = true
	updateRole(alive[1])
	end
	end
end

function clickRight()
	if spectating then
		local last = alive[watch]
		watch = 0
		alive = getAlive()
		for i,p in ipairs(alive) do if p==last then watch=i break end end
		watch = watch+1
		if watch>#alive then watch=1 end
		workspace.CurrentCamera.CameraSubject = alive[watch].Character.Humanoid
		updateRole(alive[watch])
	end
end

function clickLeft()
	if spectating then
		local last = alive[watch]
		watch = 0
		alive = getAlive()
		for i,p in ipairs(alive) do if p==last then watch=i break end end
		watch = watch-1
		if watch<1 then watch=#alive end
		workspace.CurrentCamera.CameraSubject = alive[watch].Character.Humanoid
		updateRole(alive[watch])
	end
end
			
function updateRole(player)
	if player.Character~=nil and player.Character:FindFirstChild("Humanoid")~=nil and player.Character.Humanoid:FindFirstChild("CharName")~=nil then
		script.Parent.Parent.Center.CharName.Text = player.Character.Humanoid.CharName.Value
		script.Parent.Parent.Center.PlayerName.Text = "("..player.Character.Name..")"
	if player.Character.Humanoid:FindFirstChild("Murderer")~=nil then
			script.Parent.Parent.Center.Role.Text = "Murderer"
			script.Parent.Parent.Center.Role.TextColor3 = Color3.fromRGB(255, 0, 0)
		elseif player.Character.Humanoid:FindFirstChild("Cop")~=nil then
			script.Parent.Parent.Center.Role.Text = "Sheriff"
			script.Parent.Parent.Center.Role.TextColor3 = Color3.fromRGB(255, 170, 0)
		else
			script.Parent.Parent.Center.Role.Text = "Innocent"
			script.Parent.Parent.Center.Role.TextColor3 = Color3.fromRGB(0, 255, 0)
	end
	else
		script.Parent.Parent.Center.CharName.Text = ""
		script.Parent.Parent.Center.Role.Text = ""
	end
	
end

function endSpectate()
	spectating = false
	workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
	script.Parent.Parent.Center.Visible = false
	script.Parent.Parent.ClickRight.Visible = false
	script.Parent.Parent.ClickLeft.Visible = false
end

spectating = false

script.Parent.MouseButton1Click:connect(function()
	if spectating then
		endSpectate()
	else
		startSpectate()
	end
end)

script.Parent.Parent:WaitForChild("ClickRight").MouseButton1Click:connect(clickRight)
script.Parent.Parent:WaitForChild("ClickLeft").MouseButton1Click:connect(clickLeft)
game.ReplicatedStorage.NewRound.OnClientEvent:connect(endSpectate)
game.ReplicatedStorage.EndRound.OnClientEvent:connect(endSpectate)
1 Like