Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is the rest of the code not running? thanks to the three guys that downvoted gave me a shoutout

It's not running code after the while wait() do, loop. How would i stop that, and why? I wan't a clear answer : -)

local Player = game.Players.LocalPlayer
local AmountOfCash

function CheckStats()
	
	if Player then
		local Cash = Player.leaderstats.Cash.Value
		
		return Cash
	end
end

while wait() do
	local cash = CheckStats()
	AmountOfCash = cash
end

Player.leaderstats.Cash.Changed:Connect(function(amount)
	local number = CheckStats()
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	
	local Modula = require(game.ReplicatedStorage.AbbreviateNumber)
	local text = Modula:Abbre(number)
	
	script.Parent.Cash.Text = text
	print(amount - AmountOfCash)
end)

4 comments

0

When would you want to stop while wait() do? I need the answer so I'd know and be able to help you.

0

any type of loop yelds code, anything after it don't runs untill the loop stops, you can move lines 18-28 to before the loop and it will work

0

i dont think you get what a shoutout is

0

You realise your getting downvoted BECAUSE your being rude..? I don't get why you have to "shoutout" them in your title. It makes you look like a butthole.

3 answers

No need for snark in your title, or for demands.

The problem is that when loops run and they stop everything after them from running since Lua runs from line-to-line. You should learn about coroutines or spawning threads.

The solution is simply this:

local f = coroutine.create(function()
	while wait() do
		-- do stuff
	end
end

coroutine.resume(f)

4 comments

0

Edited the answer, I forgot a line.

0

Can you please fit it in my code?

0

I won't edit the answer, but just replace lines 13 to 16 with this and put lines 14 and 15 into line 3.

0

yeah, the "( Im waiting )" is really unnecessary. heck, i even have a post from 9 months ago that never got answered! lol

You MUST put the while loop at the end. Because the script will be on repeating that loop, and will not go the next line of the script.

The only way to get the code after the while loop is to have some kind of breakout aka:

local doBreak  = false
while true do
	if(doBreak  == true) then
		break
	end
	wait()
end
--// this bit will NOT be execuited unless the doBreak becomes true! otherwise it would be run!
print("hello world!")

to make it easyer i would make changes from:

local Player = game.Players.LocalPlayer
local AmountOfCash

function CheckStats()

    if Player then
        local Cash = Player.leaderstats.Cash.Value

        return Cash
    end
end

while wait() do
    local cash = CheckStats()
    AmountOfCash = cash
end

Player.leaderstats.Cash.Changed:Connect(function(amount)
    local number = CheckStats()

    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local Modula = require(game.ReplicatedStorage.AbbreviateNumber)
    local text = Modula:Abbre(number)

    script.Parent.Cash.Text = text
    print(amount - AmountOfCash)
end)

To:

local Player = game.Players.LocalPlayer
local AmountOfCash

function CheckStats()

    if Player then
        local Cash = Player.leaderstats.Cash.Value

        return Cash
    end
end
--// moved this before the while loop so it gets hooked up
Player.leaderstats.Cash.Changed:Connect(function(amount)
    local number = CheckStats()

    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local Modula = require(game.ReplicatedStorage.AbbreviateNumber)
    local text = Modula:Abbre(number)

    script.Parent.Cash.Text = text
    print(amount - AmountOfCash)
end)

while wait() do
    local cash = CheckStats()
    AmountOfCash = cash
end


for the code you posted to work after the while loop you would need some way of breaking out of the loop but then re-call the loop after you hook up the event for your cash. something like:...

local Player = game.Players.LocalPlayer
local AmountOfCash

function CheckStats()

    if Player then
        local Cash = Player.leaderstats.Cash.Value

        return Cash
    end
end
--// ##################################
--// changed/new code
--// ##################################
local FinishedLoading = false
function mainLoop()
	while wait() do
		local cash = CheckStats()
		AmountOfCash = cash
		if(FinishedLoading == false) then
			break;
		end
	end
end
mainLoop()
Player.leaderstats.Cash.Changed:Connect(function(amount)
    local number = CheckStats()

    local ReplicatedStorage = game:GetService("ReplicatedStorage")

    local Modula = require(game.ReplicatedStorage.AbbreviateNumber)
    local text = Modula:Abbre(number)

    script.Parent.Cash.Text = text
    print(amount - AmountOfCash)
end)
FinishedLoading = true
mainLoop()
--// ###################################
--// End of new/changed code
--// ###################################

This discussion is preserved as it was. New answers, comments, and votes are closed.