Scrizonn
(Scrizonn)
November 16, 2019, 4:22pm
#1
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
metryy
(metry)
November 16, 2019, 4:32pm
#2
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
Scrizonn
(Scrizonn)
November 16, 2019, 4:35pm
#4
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
metryy
(metry)
November 16, 2019, 4:36pm
#5
Fixed my solution. Read my first reply and let me know if that works.
1 Like
Scrizonn
(Scrizonn)
November 16, 2019, 4:39pm
#6
Just tried it, it worked, thanks!