In my OOP class script, I am trying to wait for a value to exist in a table. I am trying to wait for a bindable event to fire in my __newindex function. I keep getting this annoying error:
8 attempt to yield across metamethod/C-call boundary
Here are my scripts:
--ClassModule, ReplicatedStorage
local Event = Instance.new("BindableEvent")
local Class = {}
Class.ClassName = "Class"
Class.__index = Class
Class.__newindex = function(Table, Key, Value)
if Table[Key] ~= Value then
Event.Event:Wait()
end
end
function Class.new(parent)
local newClass = {}
setmetatable(newClass,Class)
newClass.Parent = parent
Event:Fire()
return newClass
end
return Class
--ActivationScript, ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local class = require(ReplicatedStorage.Class:FindFirstChild("Class"))
local new = class.new(workspace)
You cant yield/wait in metatable functions
You can use coroutines to create a new thread and wait there however
Thanks. Do you think there is a simple alternative to waiting for the value to load? I do not want to waste time with the wait() function.
Ive never really run into problems with a module not loading properly, the class should only return the value once its properly loaded
The way __newindex works is it runs when you try to add or edit a value, it gives you the value before the initial edit I believe.
Well it does, but if you just have an empty __newindex function lua will never add the key youre requesting to add
If it sees theres a __newindex function it completely ignores the source lua code related to the creation of the key assuming you are the one whos going to create it
So yes, it kinda does it before its created because it will never be created until you do so in the function
So for an event when something is created youd have to firstly create it then do the event stuff
x.__newindex = function(tbl,key,val)
tbl[key] = val
--has been properly loaded at this point
end
Thanks, sorry for the delayed answer.
1 Like