What is causing this script to crash my studio?

I’m working on a class selection GUI, I have the following code below, and whenever I get the notification once I click “yes” my studio just crashes. Everything prints, not too sure what’s going on. I have made a lot of edits to this code to try and get it to not crash, but simply just no luck, even if I go to just stop my playtest in studio, I crash. Help would be greatly appreciated.

P.S if there’s a possible way to incorporate my table of classes with whichever class is selected, please let me know how I can do so! Thank you for reading.

local plr = game.Players.LocalPlayer
local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

local notiOS = false
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local clientToServ = ReplicatedStorage:WaitForChild("clientToServ")
local servToClient = ReplicatedStorage:WaitForChild("servToClient")

local modules = ReplicatedStorage:WaitForChild("modules")
local utilMod = require(modules:WaitForChild("LayoutUtil"))

local bg = script.Parent:WaitForChild("bg")
local holder = bg:WaitForChild("holder")
local notification = script.Parent:WaitForChild("notification")
local texts = notification:WaitForChild("texts")
local buttons = notification:WaitForChild("buttons")
local selected = "nil"

utilMod.new(holder:WaitForChild("UIGridLayout"))

local classes = {
	"discoverer",
	"scavenger",
	"consciousness",
	"survivalist"
	--THINK OF ONE MORE
}

for _, classOption in pairs(holder:GetChildren()) do
	if classOption:IsA("TextButton") then
		classOption.MouseButton1Click:Connect(function()
			if classOption.Selectable then
				selected = classOption.Name
				texts.class.Text = "<i><b>" .. string.upper(classOption.Name) .. "</b></i>"
				notiOS = true
				notification:TweenPosition(UDim2.new(0.5, 0, 0.5, 0),0.7)


			elseif not classOption.Selectable then
				local id = classOption:FindFirstChildOfClass("IntValue")
				game.MarketplaceService:PromptGamePassPurchase(plr, id.Value)
			end
		end)
	end
end

for _, option in pairs(buttons:GetChildren()) do
	if option:IsA("TextButton") then
		option.MouseButton1Click:Connect(function()
			notification:TweenPosition(UDim2.new(0.5, 0, -0.5, 0),0.7)
			notiOS = false
			
			if option.Name == "yes" then
				if selected ~= "nil" then
					if selected == "discoverer" or selected == "scavenger" or selected == "consciousness" or selected == "survivalist" then--or selected == "discoverer" then
						clientToServ:FireServer("selectClass", string.lower(texts.class.Text))
						print("fired")
						clientToServ:FireServer("deleteUI", "classSelection")
						print("done")
					end
				end
			end
		end)
	end
end

Hello, there are too many operations, you need to insert wait() inside of loops, or game:GetService("RunService").RenderStepped:Wait() since it waits every frame to update.

1 Like

Where would be the most efficient place to put this?

Depending on where is more objects which you loop.

I accidentally added a “,” to a section of my DataModule that shouldn’t have been there, that was the culprit.