How do I update a Variable referring to a Leaderstat? [FIXED]

Hi Other Developers,
My variable won’t change when I change the leaderstat, here is my code below:

local ButtonsFolder = game:GetService("Workspace"):WaitForChild("Buttons")
local ModuleScript = require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))

game.Players.PlayerAdded:Connect(function(Player)
	
	print("Player Joined")
	local Leaderstats = Player:WaitForChild("leaderstats")
	local Cash = Leaderstats:WaitForChild("Cash")
	
	local CheckCashValue = coroutine.wrap(function()
		while true do
			print(Cash.Value)
			task.wait(3)
		end

	end)
	
	CheckCashValue()
	
	for Button in ipairs(ButtonsFolder:GetChildren()) do 
		local ButtonInstance = ButtonsFolder:WaitForChild("Button" .. tostring(Button))
		ButtonInstance:SetAttribute("Enabled", true)
		
		ButtonInstance.Touched:Connect(function()
			
			if Cash.Value - ModuleScript[tostring(ButtonInstance)].Price >= 0 and ButtonInstance:GetAttribute("Enabled") then
				
				ButtonInstance:SetAttribute("Enabled", false)
				ButtonInstance.Transparency = 1
				ButtonInstance.CanCollide = false
				ButtonInstance.CanTouch = false
				ButtonInstance:WaitForChild("BillboardGui").Enabled = false
				
				Cash.Value -= ModuleScript[tostring(ButtonInstance)].Price
				
			end
			
		end)

	end
	
end)

When I change the cash leaderstat, the cash in this script does not change, the coroutine function will still print the previous value of cash.

Help would be greatly appreciated!

1 Like

Your ‘CheckCashValue’ is looping before the updating of the cash value you need to put the loop in spawn function so it’s in a seperate thread and wont stop the cash value from bein updated.

local ButtonsFolder = game:GetService("Workspace"):WaitForChild("Buttons")
local ModuleScript = require(game:GetService("ReplicatedStorage"):WaitForChild("ModuleScript"))

game.Players.PlayerAdded:Connect(function(Player)
    print("Player Joined")
    
    local Leaderstats = Player:WaitForChild("leaderstats")
    local Cash = Leaderstats:WaitForChild("Cash")

    spawn(function()
        while true do
            print(Cash.Value)
            wait(3)
        end
    end)

    for _, ButtonInstance in ipairs(ButtonsFolder:GetChildren()) do
        local Price = ModuleScript[tostring(ButtonInstance)].Price

        ButtonInstance:SetAttribute("Enabled", true)

        ButtonInstance.Touched:Connect(function()
            if Cash.Value - Price >= 0 and ButtonInstance:GetAttribute("Enabled") then
                ButtonInstance:SetAttribute("Enabled", false)
                ButtonInstance.Transparency = 1
                ButtonInstance.CanCollide = false
                ButtonInstance.CanTouch = false
                ButtonInstance:WaitForChild("BillboardGui").Enabled = false

                Cash.Value = Cash.Value - Price
            end
        end)
    end
end)

This code uses spawn to create a new thread for the loop, and it simplifies the button iteration using ipairs. Ensure that the Cash value is being updated correctly when you make changes elsewhere in your game logic.

2 Likes

Yes, but it still doesn’t still work after I change the variable,
for example, I first purchased the a button, then change my leaderstat value to be enough for the second button, but it doesn’t purchase it for some reason.

try debugging by using print, also since this is a server script there is no need for waitforchild function

local ButtonsFolder = game:GetService("Workspace").Buttons
local ModuleScript = require(game:GetService("ReplicatedStorage").ModuleScript)

game.Players.PlayerAdded:Connect(function(Player)
	
	print("Player Joined")
	local Leaderstats = Player:WaitForChild("leaderstats")
	local Cash = Leaderstats:WaitForChild("Cash")
	
	local CheckCashValue = coroutine.wrap(function()
		while true do
			print(Cash.Value)
			task.wait(3)
		end

	end)
	
	CheckCashValue()
	
	for i, Button in pairs(ButtonsFolder:GetChildren()) do 
		Button:SetAttribute("Enabled", true)
		local Price = ModuleScript[Button.Name].Price

		Button.Touched:Connect(function()
			print(Cash.Value, Price)

			if Cash.Value - Price >= 0 and Button:GetAttribute("Enabled") then
				
				Button:SetAttribute("Enabled", false)
				Button.Transparency = 1
				Button.CanCollide = false
				Button.CanTouch = false
				Button.BillboardGui.Enabled = false
				
				Cash.Value -= Price
				
			end
			
		end)

	end
	
end)
1 Like

the print for the Price variable works, but not the Cash Value for some reason.

If you’re using a LocalScript (which I’m assuming you are since you’re using WaitForChild) then the issue might be that the cash value is changing on the client but not on the server. Also, if you are using a LocalScript then there’s also a problem where some buttons might not exist yet when the loop runs so I suggest you edit the script like so (this doesn’t solve the first problem I mentioned though as you’ll need to use a RemoteEvent and a new server script to do so):

local ButtonsFolder = game:GetService("Workspace").Buttons
local ModuleScript = require(game:GetService("ReplicatedStorage").ModuleScript)

game.Players.PlayerAdded:Connect(function(Player)
	
	print("Player Joined")
	local Leaderstats = Player:WaitForChild("leaderstats")
	local Cash = Leaderstats:WaitForChild("Cash")
	
	local CheckCashValue = coroutine.wrap(function()
		while true do
			print(Cash.Value)
			task.wait(3)
		end

	end)
	
	CheckCashValue()
			
		end)

	end
	
end)

local Cash = game.Players.LocalPlayer:WaitForChild"leaderstats":WaitForChild"Cash"

local function buttonTouch(Button)
        Button:SetAttribute("Enabled", true)
		local Price = ModuleScript[Button.Name].Price

		Button.Touched:Connect(function()
			print(Cash.Value, Price)

			if Cash.Value - Price >= 0 and Button:GetAttribute("Enabled") then
				
				Button:SetAttribute("Enabled", false)
				Button.Transparency = 1
				Button.CanCollide = false
				Button.CanTouch = false
				Button.BillboardGui.Enabled = false
				
				Cash.Value -= Price
end end

for i, Button in pairs(ButtonsFolder:GetChildren()) do buttonTouch(Button) end
		
ButtonsFolder.ChildAdded:Connect(buttonTouch)

Edit: Forgot to add the Cash value to the function so I fixed it now

2 Likes

Oh oops, im using a serverscript which is inside ServerScriptService, must be muscle memory or something sorry.

1 Like

Can you show us the ButtonsFolder in the Explorer with what you have inside of it please, so that I can try writing a new script. I’m asking you this because I noticed something odd inside of the for loop

Also, if you show us the code inside the Module that would be very helpful as well because the issue could be due to it