How do I constantly update a variable?

function SyncToAttribute(target, name, variable)
	local function sync()
		variable = target:GetAttribute(name)
	end
	sync()
	target:GetAttributeChangedSignal(name):Connect(sync)
end

while true do 
wait(1)

	local attri = 0
    
	SyncToAttribute(RS.Economy.Modules.calculate_budget, "legion_salary", attri)
    print(attri)

end

I need a variable to be updated, this for some reason doesn’t work? Does anyone know why? What it does is still print the old value (0) instead of the new value.

path
image

attribute of calculate_budget
image

EDIT: If anyone got a alternative to this, I’m happy to try it out and this might just be a roblox bug or something weird

If you want to update a value constantly, I’d recommend using RunService if its a LocalScript, otherwise I would recommend using a while task.wait() loop.

If you want a variable to update and continue to use the script for other purposes (you want the loop to run but not stop the script), then use task.spawn(function() … end) so that it runs in its own thread. You can find more info here.

1 Like

You can use RunService in a server script, just not RenderStepped.

1 Like

Issue is, is that the variable is still not updating even after I clearly update it with this line of code,

variable = target:GetAttribute(name) `

It still prints 0 even after it gets a different value

Is it possible that the attribute is only changing on the client side and the server side remains unchanged?

2 Likes

This is all happening on the serverside

1 Like

Have you tried printing just the output of :GetAttribute() like print(target:GetAttribute(name)) so?

Could it be possible that a certain part of the script is causing the code to iterate infinitely (such as a while loop) earlier in the script?

2 Likes

It works, if I do the GetAttribute without putting it in a variable, which is kind of annoying to be honest

1 Like

Have you tried using _G yet? Using the global table might do it.

1 Like

Yeah, I’m considering using that in my final iteration of what I’m doing, main reason why I’m using attributes is because they replicate to the client which _G does not

1 Like

Try using an IntValue in the workspace if you need to replicate a value from the client to the server.

1 Like

Attributes are like the same thing but instead of being a separate instance they’re a property of a instance which makes the code more organized imo.

Also side note the thing I was confused about attributes is that I assumed that everytime you referenced the variable with a GetAttribute function in it, it would call it, which it does not it only calls it 1 time and then doesn’t anymore.

2 Likes

It does update the variable. But the variable it updates is called variable, not attri.
If you want to update attri, you need to forward-declare it like so:

local attri = 0
function SyncToAttribute(target, name, variable)
	local function sync()
		attri = target:GetAttribute(name)
	end
	sync()
	target:GetAttributeChangedSignal(name):Connect(sync)
end

while true do 
wait(1)

	attri = 0 -- remove local
    
	SyncToAttribute(RS.Economy.Modules.calculate_budget, "legion_salary", attri)
    print(attri)

end

This is a bad practice since it takes up more memory and processing power than it needs to (especially since in your example code it creates a whole new variable/table per attribute every loop), but you can do this.

function SyncToAttribute(target, name, variable)
	local function sync()
		variable[1] = target:GetAttribute(name)
	end
	sync()
	target:GetAttributeChangedSignal(name):Connect(sync)
end

while true do 
wait(1)

	local attri = {0}
    
	SyncToAttribute(RS.Economy.Modules.calculate_budget, "legion_salary", attri)
    print(attri[1])

end
2 Likes

This also doesn’t make sense; every single second of the loop it’s creating a new changed signal. That’s unnecessary. It only needs to be done once and then can update the value in a table, if needed.

local attributes = {};

local function SyncToAttribute(target, name)
	attributes[target] = {};
	attributes[target][name] = target:GetAttribute(name);
	
	target:GetAttributeChangedSignal(name):Connect(function()
		attributes[target][name] = target:GetAttribute(name);
	end)
end

SyncToAttribute(RS.Economy.Modules.calculate_budget, "legion_salary");

Then if for some reason you need to access it in a loop, you’d just do:

while task.wait(1) do
	print(attributes[RS.Economy.Modules.calculate_budget]["legion_salary"]);
end

In the case of the OP though, it seems like all they want is to get the attribute every second, which means that this is a massive overcomplication and all they really need is just this part (the rest is kinda pointless in this scenario):

while task.wait(1) do
	local attri = RS.Economy.Modules.calculate_budget:GetAttribute("legion_salary");
	print(attri);
end
4 Likes

Wasn’t In my example “variable” the parameter in the function and the parameter I inserted for “variable” was attri. I don’t see how that wouldn’t work?

The variable can only be updated in its own scope.

When you pass attri to the function you created here, you’re only passing the 0. It has no reference to the actual variable named “attri”.

So “variable” here is literally just the number 0.

1 Like

The reason it doesn’t work (but my table example does) is the = symbol.
Take a look at this

variable = attri
variable = 5

What now is attri? It could be 5 if the developers of Lua had wished it, but that actually makes less sense than you think. There are plenty of reasons in computer science why you would want to move variables around and overwrite them. Here’s a good example.

local startNum = 10

local function timer ()
    local currentNum = startNum
    repeat
        currentNum -= 1
        wait(1)
    until
        currentNum == 0
end

In this example, startNum remains unchanged.

This always happens whenever you use = to assign a variable. In my table example, I didn’t use = to assign the variable, I instead used variable[1] to assign something to a value in the table. Thus attri and variable still equaled the same table.

The reason it doesn’t work (but my table example does) is the = symbol.
Take a look at this

attri = 7
variable = attri
variable = 5

What now is attri? It could be 5 if the developers of Lua had wished it, but that actually makes less sense than you think. There are plenty of reasons in computer science why you would want to move variables around and overwrite them. Here’s a good example.

local startNum = 10

local function timer ()
    local currentNum = startNum
    repeat
        currentNum -= 1
        wait(1)
    until
        currentNum == 0
end

In this example, startNum remains unchanged.

This always happens whenever you use = to assign a variable. In my table example, I didn’t use = to assign the variable, I instead used variable[1] to assign something to a value in the table. Thus attri and variable still equal to the same table.

1 Like