Global DB not functioning?

Hello, I’m trying to make a global DB, but once i try to call the db out of the function it was made it, it won’t
recognize it

local Players = game:GetService("Players")
 _G.BARRAGEDB = {}

Players.PlayerAdded:Connect(function(plr)
   _G.BARRAGEDB[plr]= true
end)
Players.PlayerRemoving:Connect(function(plr)
    _G.BARRAGEDB[plr] = nil
end)


print(_G.BARRAGEDB[plr---this would be underlined red])
2 Likes

The script you posted does not know what plr means because it’s limited to the scope of where it’s defined which is in your script the PlayerAdded and PlayerRemoving function callbacks. If you want to make it print whenever a new value is added, you can use the __newindex metamethod by calling the setmetatable function and setting the __newindex metamethod to print.

To do this, replace _G.BARRAGEDB = {} with:

 _G.BARRAGEDB = setmetatable({}, {__newindex = print})
2 Likes

Here’s what it looks like now, it still doesn’t recognize the function

local Players = game:GetService("Players")
 _G.BARRAGEDB = {}

Players.PlayerAdded:Connect(function(plr)
   _G.BARRAGEDB[plr]= true
end)
Players.PlayerRemoving:Connect(function(plr)
    _G.BARRAGEDB[plr] = nil
end)


print(_G.BARRAGEDB[plr])---plr is underlined as red

Fixed my solution. Read my first reply and let me know if that works.

1 Like

Just tried it, it worked, thanks!