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