Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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()

5 comments

0

is your "data" variable not changing? If not then I can answer

0

yes

0

So the issue is that the data variable isn't updating?

0

yea

0

If i helped you out can you please accept my answer? It means a lot

1 answer

✓ Accepted answer

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.

2 comments

0

incapaz

0

thank you so much

This discussion is preserved as it was. New answers, comments, and votes are closed.