Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-3

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

Asked by 3 years ago
Edited 3 years ago

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)
0
When would you want to stop while wait() do? I need the answer so I'd know and be able to help you. Gabe_elvin1226aclan 323 — 3y
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 Leamir 3138 — 3y
0
i dont think you get what a shoutout is zadobyte 692 — 3y
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. GAM3RBOY2008 0 — 3y

3 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
Edited the answer, I forgot a line. radiant_Light203 1166 — 3y
0
Can you please fit it in my code? CaIcuIati0n 246 — 3y
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. radiant_Light203 1166 — 3y
0
yeah, the "( Im waiting )" is really unnecessary. heck, i even have a post from 9 months ago that never got answered! lol GAM3RBOY2008 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
3 years ago

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
--// ###################################

Answer this question