If you're trying to create an admin, this is how I would do it.
:GetAsync() has 1 argument and returns a table. The one argument is the player you're trying to get data for.
:SetAsync() has 2 arguments and saves the data as a table. The 2 arguments are, first being the player you're saving for, and second being the data you're saving.
I would just save the data to the player leaving, and be updated when they rejoin back.
I'm using a ServerScript inside of ServerScriptService with a ModuleScript inside of that ServerScript.
Inside of the ServerScript, I wrote this.
repeat wait() until require(script.AdminModule)
This will insure that the module has been acquired and will run when the game starts.
Inside of the ModuleScript, I wrote this.
local adminModule = {}
local https = game:GetService("HttpService")
local DSS = game:GetService("DataStoreService")
local ADS = DSS:GetDataStore("data1")
local BDS = DSS:GetDataStore("data2")
local admins = {"YourNameHere"}
local bans = {"SomeonesNameHere"}
game.Players.PlayerAdded:Connect(function(player)
local key = player.UserId
local adminEncodedData = ADS:GetAsync(key)
local adminData = https:JSONDecode(adminEncodedData)
admins = adminData
print(adminEncodedData)
local banEncodedData = BDS:GetAsync(key)
local banData = https:JSONDecode(banEncodedData)
bans = banData
print(banEncodedData)
local auto_save = coroutine.wrap(function()
while wait(60) do
if DSS:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync) > 0 then
local success, err = pcall(function()
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
local key = player.UserId
local adminData = https:JSONEncode(admins)
ADS:SetAsync(key,adminData)
print(adminData)
local banData = https:JSONEncode(bans)
BDS:SetAsync(key,banData)
print(banData)
end
end)
end
end
end)
auto_save()
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local adminData = https:JSONEncode(admins)
ADS:SetAsync(key,adminData)
print(adminData)
local banData = https:JSONEncode(bans)
BDS:SetAsync(key,banData)
print(banData)
end)
game:BindToClose(function()happened, like their pc turned off.
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
local key = player.UserId
local adminData = https:JSONEncode(admins)
ADS:SetAsync(key,adminData)
print(adminData)
local banData = https:JSONEncode(bans)
BDS:SetAsync(key,banData)
print(banData)
end
end)
return adminModule
So, how this works is...
I used 2 data stores, one for bans, and another for admins. Just incase you would want to reset the admins or bans. Then, all the data is formatted into Decoded HTTP JSON Table, so you can print the data and set the bans and admin table to the requested data. I created an autosave which checks if the data CAN be saved, and if it can, then it will save everyone's data. After that, I had to save the data when the player leaves using Encoded HTTP Formatted JSON Table, since that's how we decoded it, but also encode it. I usually print the data, just for a tests. And the game:BindToClose is ran whenever the game shuts down unexpectedly, for example if you force shutdown all servers, or their pc turns off, it will save the data, which is also Encoded HTTP Formatted JSON Tables.
Hope this made sense!
Some of this stuff isn't really necessary, but I just kept it.
Keep in mind, this is an old script of mine that still functions.
This isn't the best admin, but it does what you're asking. It loads and saves a table, assuming that it's for admin since it's a ban table.
Hope this helped you with your journey!
1 comment
:SetAsync() has 2 arguments. The first one being the data it's being saved to, and the second one being the data saved. :SetAsync(player, data).