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
attribute of calculate_budget
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.
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
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.
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
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
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 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.