Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to update Data Value in a function?[Solved]

Asked by 5 years ago
Edited 5 years ago

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()
0
is your "data" variable not changing? If not then I can answer User#24403 69 — 5y
0
yes superbolt999 36 — 5y
0
So the issue is that the data variable isn't updating? User#24403 69 — 5y
0
yea superbolt999 36 — 5y
0
If i helped you out can you please accept my answer? It means a lot User#24403 69 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

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.

Reference vs Value

It is important that you know the difference between a reference and a value.

Reference

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.

Value

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.

Final product

local numb = game.Workspace.Numb

local function example()
    while numb.Value == 0 do
        print("same value")
    end
    print("value = 1")
end
example()


Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.
0
incapaz Optikk 499 — 5y
0
thank you so much superbolt999 36 — 5y
Ad

Answer this question