I tried to get the data value but the script won't update when it changes. For example, while the output is printing same value, I changed the numb value to 1 but the script still thinks that numb.value = 0. Here is my script(It has to be a function)
function example() data = game.Workspace.Numb.Value while data == 0 do print("same value") end print("value = 1") end example()
The problem here is the fact that you wrongly assume that the data
variable is a reference to the property of the Numb
.
To solve this problem, you would have the variables hold the IntValue/NumberValue
object itself, and make a direct access to the Value
property.
It is important that you know the difference between a reference
and a value
.
In Lua, for example, tables are passed by reference.
local tbl1 = {"hello", "world", "foo", "bar"} local tbl2 = {"hello", "world", "foo", "bar"} local tbl1reference = tbl1 table.insert(tbl1, "test") for _, v in ipairs({tbl1, tbl2, tbl1reference}) do print(table.concat(v, ", ")) end --[[ output: hello, world, foo, bar, test hello, world, foo, bar hello, world, foo, bar, test --]]
tbl1
and tbl1reference
hold the same exact table in memory and tbl2
does not, even if it has the same values in the same positions. Changes made to tbl1
or tbl1reference
will affect tbl1
and tbl1reference
.
However things like booleans and numbers are passed simply via value.
local _string = "hello" local _string_value = _string _string = "world" print(_string_value) local bool1 = false local bool1_val = bool1 bool1 = true print(bool1) --> true print(bool1_val) --> false
Even though _string_value
was set to _string
, both variables have different memory and modifying one value does not change the other.
local numb = game.Workspace.Numb local function example() while numb.Value == 0 do print("same value") end print("value = 1") end example()