--script2
while not _G["ExampleValue"] do
wait()
end
print(_G["ExampleValue"])
_G is a table that can be accessed across all scripts
(Client → Client)
(Server → Server)
probably the easiest way.
this is because a module will create a copy of the returned value any time it gets required
unless its from the same script, then you will get the cached result
So Basically, there is a usage of CollectionService that applies .Touched Event, from there it accesses the Player fine, every seems to work as intended, but when atempting to Access the Data on the Script, it returns nil, which ends up causing an error.
The Part of Code is literally just:
local Data = ModuleScript.CurrentData[Player]
It also doesnt work if I create a whole function to gab the Data.
The Data when testing in Empty Baseplates works just fine after remaking the code, however here is for some reason an exception.
You’ll have to use a BindableFunction to grab the module environment from a Main script, this is just an example but the way I do it is
function getServerSettings()
return ServerSettings;
end
function getDataService()
return DataManager;
end
local External = Instance.new("BindableFunction");
External.Parent = script;
External.OnInvoke = function(Fnc, ...)
return getfenv(0)[Fnc](...)
end
and to use it you’d just need to call External:Invoke(“nameOfTheFunction”);
If I’m understanding this correctly, There seems to be a race condition happening. Whenever you try to access the player data, it hasn’t been created yet, so it returns a nil value. What I normally do with this is create a function that will fetch the cached data instead of accessing the CurrentData directly.
function module.fromCache(Player)
while not module.CurrentData[Player] and PlayerService:FindFirstChild(Player.Name) do -- Prevent infinite loop if the player leaves.
task.wait()
end
return module.CurrentData[Player]
end
--script
local Data = module.fromCache(Player)
if Data then
print(Data)
end
You can also use signals instead of task.wait() but nonetheless its still the same.