ifConfirm() function possibly crashing ROBLOX Studio?

So, after finally getting my rank shop code to not error, I now face another annoying issue. For some reason, when I want to purchase a rank and I click the purchase button, it freezes up studio; Most of the time it causes it to crash, but one time it didn’t, but instead it took excess cash from my cash value within the player, and I’m stuck on what could be causing it. I know for definite that the shop is causing this though. Help?

Code:

local allNames = require(game.ReplicatedStorage.ListOfAllRanks)
local player = game.Players.LocalPlayer
local CurrentlyEquipped = player:WaitForChild("CE")
local buttons = {}
local owned = {}

game.ReplicatedStorage.SendOwnedThings.OnClientEvent:Connect(function(ownedStuff)
	owned = ownedStuff
end)
for i, v in pairs(allNames) do
	local Button = Instance.new("TextButton", script.Parent)
	Button.BackgroundColor3 = Color3.new(1, 0.333333, 0) 
	Button.BorderColor3 = Color3.new(1, 0.333333, 0) 
	Button.TextColor3 = Color3.new(1, 1, 1)
	Button.TextScaled = true
	Button.Font = "PermanentMarker"
	Button.Name = tostring(i)

	local UiCorner = Instance.new("UICorner", Button)
	table.insert(buttons, #buttons+1, Button)
	Button.Text = v.Name
	local HasGot = Instance.new("BoolValue", Button)
	HasGot.Name ="HasGot"
	HasGot.Value = false
	local Price = Instance.new("IntValue", Button)
	Price.Name = "Price"
	
	Price.Value= v.Price

end
if owned ~= nil then
	for _, v in pairs(buttons) do
		for _, o in pairs(owned)do
			if v.Text == o  then
				v:WaitForChild("HasGot").Value = true
			end
		end
	end
end


function EquipFire(text)
	game.ReplicatedStorage.EquipRank:FireServer(text)
end
function ifConfirm(stringGiven, price)
	game.ReplicatedStorage.BuyRank:FireServer(price)
	script.Parent.Parent["Buy/Equip"].Text = "Equip"
	script.Parent.Parent["Buy/Equip"].TouchTap:Connect(function()
		EquipFire(stringGiven)
	end)
	script.Parent.Parent["Buy/Equip"].MouseButton1Down:Connect(function()
		EquipFire(stringGiven)
	end)
	if owned~= nil then
		table.insert(owned, #owned+1, stringGiven)
	else
		local tb = {}
		table.insert(tb, #tb+1, stringGiven)
		owned = tb
	end
	
	for i , v in pairs(script.Parent:GetChildren()) do
		if v:IsA("TextButton") then
			if v.Text== stringGiven then
				v:WaitForChild("HasGot").Value =true
			end
		end
	end
	game.ReplicatedStorage.UpdateOwned:FireServer(owned)
	script.Parent.Parent["Buy/Equip"].MouseButton1Down:Connect(EquipFire(stringGiven))

end

function Touched(button)
	if not button:WaitForChild("HasGot").Value then
		script.Parent.Parent.SelectedOneHere.Text = button.Text
		script.Parent.Parent["Buy/Equip"].Text = "Do You Want To Buy this?"
		script.Parent.Parent.Price.Text = "Price = "..button.Price.Value
		local function OnClick()
			ifConfirm(button.Text, button.Price.)
		end
		script.Parent.Parent["Buy/Equip"].TouchTap:Connect(function()
			ifConfirm(button.Text, button.Price.Value)
		end)
		script.Parent.Parent["Buy/Equip"].MouseButton1Down:Connect(function()
			ifConfirm(button.Text, button.Price.Value)
		end)
		
	elseif not CurrentlyEquipped.Value  == button and button:WaitForChild("HasGot").Value then
		script.Parent.Parent.SelectedOneHere.Text = button.Text
		script.Parent.Parent["Buy/Equip"].Text = "Unequipped"
	elseif CurrentlyEquipped.Value == button.Text and button:WaitForChild("HasGot").Value then
		script.Parent.Parent.SelectedOneHere.Text = button.Text
		script.Parent.Parent["Buy/Equip"].Text = "Equipped"
	end


end

while wait() do
	for i, v in pairs(buttons) do
		v.TouchTap:Connect(function()
			Touched(v)
		end)
		v.MouseButton1Down:Connect(function()
			Touched(v)
		end)
	end
end

It’s this loop that’s causing your studio to crash. What is the point of having a loop for this? Connecting each event once should be sufficient.

1 Like

I had the loop so I could detect if any of the buttons were clicked in one script only. If i looped through it once i dont think it would work after the loop finished

What do you mean? The loop is most definitely not the solution as you’re just spamming connections.
For example, if you run the game, wait 5 seconds, and then click the button, you’re going to be running 5,000+ functions at once because of the loop connecting the same function to the event every single wait(). (This is assuming that wait() would take place only 1,000 times in a second, which is probably being generous)

Try it without the loop and let me know what happens.

2 Likes

Thanks! I had no idea the loop would cause it. I’ll avoid doing something like that again

1 Like