:WaitForChild() equivalent for values in a dictionary?

Is there a function equivalent of :WaitForChild() for dictionaries?


For example, I’d like to achieve something like this:

local dictionary = {}

spawn(function()
	wait(2)
	dictionary.ForeverHD = "Hello world"
end)

local value = dictionary.ForeverHD or dictionary:WaitForChild("ForeverHD")
print(value)

At the moment, my best option is doing this:

local dictionary = {}

local function getValue(valueName)
	if not dictionary[valueName] then
		repeat 
			wait() 
		until dictionary[valueName]
	end
	return dictionary[valueName]
end

spawn(function()
	wait(2)
	dictionary.ForeverHD = "Hello world"
end)

local value = getValue("ForeverHD") 
print(value)
1 Like

I don’t see why you would ever need to do this. You are setting the value somewhere else in your code, so you already know when it’s going to appear and what it’s going to be.

2 Likes

You are in control of creating keys in that dictionary, so you should already be aware of when any key is or is not set. It doesn’t make much sense to need something like this. What are you trying to do with this exactly?

Edit: ninja’d

2 Likes

I store some values in a module script in ReplicatedStorage. These values are only added in and updated by the server. In ReplicatedFirst I have a LocalScript which needs these values hence the need to wait for them.

The returned value from require(repStore.SomeModule) on the server is not the same as the return value from require(repStore.SomeModule) on the client, or as between clients. It sounds like this is the bigger issue here.

5 Likes

If u rlly wanted to I think u could bind __index to wait for the key to point to a value

Seems pointless tho

Edit: Apparently it can’t yield rip

1 Like

That’s a good point - I’ll have to rethink this data storage.

Thanks

In other cases you can use bindable events or you can use setters, updaters, or proxy tables so that things get called when changing its value. Also, the modules are supposed to work like that otherwise it would be a major security flaw.

1 Like

__index metamethod cannot yield iirc.

2 Likes