Script not continuing after function

I have made a script but after i run a function it wont run anything else in the script.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats"):WaitForChild("Cash")
local leaderstats2 = Player:WaitForChild("leaderstats"):WaitForChild("Rank")
local PlayerGui = Player:WaitForChild("PlayerGui")
local Main = PlayerGui:WaitForChild("MainUI")
function Comma(amount)
	while true do
		amount, k = string.gsub(amount, "^(-?%d+)(%d%d%d)", '%1, %2')
		if (k == 0) then
			break
		end
	end
	return amount
end

Main.LeftMain.Cash.Container.Container.CashAmount.Text = "$"..Comma(leaderstats.Value)
leaderstats.Cash:GetPropertyChangedSignal("Value"):Connect(function()
	Main.MainUI.LeftMain.Cash.Container.Container.CashAmount.Text = "$"..Comma(leaderstats.Value)
end)
-- Nothing will run after this point

Main.LeftMain.XP.Container.Icon.RankLabel.Text = "$"..Comma(leaderstats2.Value)
leaderstats.Rank:GetPropertyChangedSignal("Value"):Connect(function()
	Main.MainUI.LeftMain.XP.Container.Icon.RankLabel.Text = "$"..Comma(leaderstats2.Value)
end)
PlayerGui:WaitForChild("MainUI").PhoneMain.MouseEnter:Connect(function()
	PlayerGui:WaitForChild("MainUI").PhoneMain:TweenPosition(
		UDim2.new(0, 10,1, -20),
		"Out",
		"Quad",
		.2,
		true -- Enables overright (don't recommend using it a script controlling the entire gui)
		-- Good to put in a seperate script	
	)
end)





1 Like

Does this work to you? Does it change the text etc?

This is probably happening because the function Comma runs an infinite loop, which will prevent the rest of your script from running until it stops.

maybe your while loop never ending

task.spawn(function()
    function Comma(amount)
	while true do
		amount, k = string.gsub(amount, "^(-?%d+)(%d%d%d)", '%1, %2')
		if (k == 0) then
			break
		end
	end
	return amount
end
end)

Also I would add a task.wait() for your while true loop. You’re going to run into script exhaustion

Im not sure if the while true do Stops running so wrap it in a coroutine so your code will look like this

function Comma(amount)
coroutine.resume(coroutine.create(function()
	while true do
		amount, k = string.gsub(amount, "^(-?%d+)(%d%d%d)", '%1, %2')
		if (k == 0) then
			break
		end
	end
	return amount
end))
end

That comma function started breaking for me too, once you called it. The variable k doesn’t seem to hit 0 anymore essentially creating an unbreakable loop. This should help you. It’s a different way to add commas into your number without it running a loop.