I can't use metatables with exports

readonly function working on export file but not working on other file

export file

function readonly(tbl)
	return setmetatable({}, {
		__index = tbl,
		__metatable = "readonly",
		__newindex = function()
			error("Attempt to modify read-only table")
		end
	})
end

exports("readonly", readonly)

other file

local test = exports["resource"]:readonly({
     ["paris"] = "france"
})

test["washington"] = "america"

-- THIS SHOULD GIVE ERROR ON SET NEW INDEX

Maybe a dumb quesrion but in other file, is the [“resource”] your resource name ?

Yes, i wrote it that way to serve as an example.

1 Like

Did you add it in the fxmanifest ? Example : client_export “myExportFct” ( or server, depending on which side ) ( on the client side it might be export and not client_export, im not sure, try both )

1 Like

Exports running correctly, It just readyonly function cant working properly.

1 Like

Ooooh ok mb misunderstood the problem, have tried defining your array before using it ? I know sometimes ( idk why ) it doesnt accept the data but only a variable poiting to the data

Like
Local tbl = {
– content
}
and then local test = exports:readonly(tbl)

1 Like

i tried this, dont work for me

1 Like

So everything works fine in the first file where it is created but in the other one the function just doesnt work ? It’s so weird…

1 Like

Or what you can try is like creating an obj ( like ESX ) and you instantiate in the other file, it wont be an export but maybe that could work ( if there’s an issue with the export it could be something to consider )

1 Like

Oh wait no, got a better idea

In the fxmanifest of the resource you want to use the export in, just add @theExportRss/theFileWithTheFunction.lua

And then you can call readonly normally without any exports stuff

1 Like

As far as I am aware, this is a limitation on the runtime side.
While metatables exist in Lua, C# or js do not have those in the same sense.

I’ve seen this solution before and I know it works. I just wondered if there was another way and wanted to ask you. Thank you anyway.