Our script used to work perfectly fine - and for some reason, it won’t automatically update anymore.
This is the localscript we use to fetch the $ and display it on the user’s GUI for their “wallet”
function addComma(int)
local left, num, right = string.match(int, "^(%D*%d)(%d*)(.-)$")
return left .. num:reverse():gsub("(%d%d%d)", "%1,"):reverse() .. right
end
local wallet = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money")
repeat wait() until wallet
script.Parent.Text = "$"..addComma(wallet.Value)
wallet.Changed:connect(function()
script.Parent.Text = "$"..addComma(wallet.Value)
end)
Any help would be highly appreciated - it appears to work sometimes, but most commonly; it doesn’t.
Nothing that I have noticed thus far, I just applied a small change to the script. I will make sure to keep an eye on the dev console next time this occurs. But from what I recall; when this issue first began, there was nothing showing any errors in the dev console.
The script was originally:
function addComma(int)
local left, num, right = string.match(int, "^(%D*%d)(%d*)(.-)$")
return left .. num:reverse():gsub("(%d%d%d)", "%1,"):reverse() .. right
end
repeat wait() until script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money")
script.Parent.Text = "$"..addComma(script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money").Value)
script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money").Changed:connect(function()
script.Parent.Text = "$"..addComma(script.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money").Value)
end)
And it is now:
function addComma(int)
local left, num, right = string.match(int, "^(%D*%d)(%d*)(.-)$")
return left .. num:reverse():gsub("(%d%d%d)", "%1,"):reverse() .. right
end
local wallet = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money")
repeat wait() until wallet
script.Parent.Text = "$"..addComma(wallet.Value)
wallet.Changed:connect(function(newmoney)
script.Parent.Text = "$"..addComma(wallet.Value)
end)
I suppose we’ll ride this out and see if this issue continues.
local wallet = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money")
My guess would be the above code along with the repeat loop you have. Lets say the money value didn’t load in when you attempted to reference it. The variable wallet would now be nil.
Now if we look at the repeat loop we can see its repeating until the wallet value exist:
repeat wait() until wallet -- Since wallet is nil its going to keep looping!
There would be no errors because its constantly looping. A quick fix for it would either to use WaitForChild(“money”) instead of FindFirstChild or to redefine the wallet variable inside the repeat loop (I don’t recommend doing this):
local wallet = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money")
repeat
wallet = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money") -- basically "updates" the wallet variable
wait()
until wallet
As I stated before I don’t recommend using a repeat wait() loop to pause a thread till something exist. You should just use :WaitForChild() and remove the loop completely!
Hello there, You could use :GetPropertyChangedSignal() to update the currency
function addComma(int)
local left, num, right = string.match(int, "^(%D*%d)(%d*)(.-)$")
return left .. num:reverse():gsub("(%d%d%d)", "%1,"):reverse() .. right
end
local wallet = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent:FindFirstChild("money")
repeat wait() until wallet
script.Parent.Text = "$"..addComma(wallet.Value)
wallet:GetPropertyChangedSignal("Value"):Connect(function()
script.Parent.Text = "$"..addComma(wallet.Value)
end)
Awesome - I’m going to try this out and see how it works, because I just had it break again. Not a single error in dev console. Neither client nor server.
We’ve never had an issue with the addComma function before - but now it appears that the Changed event is failing to fire after a certain amount of time the server is up.
I’ve tried the alternative that @Pixeluted provided - and it appears to still break.
try adding some print statements to see the name and value of wallet after the wait function.
My guess is the path changed somehow and it is hard to tell if you are using enough parents.
Since you are just changing a UI and it is a local script try doing it this way:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local wallet = player.leaderstats:WaitForChild("money") --or where ever the path is
local text = script.Parent
text.Text = "$"..wallet.Value --test without the comma function first to see if it is the problem
wallet.Changed:Connect(function()
text.Text = "$"..wallet.Value
end)
if that works, try to add the comma function and test again.