Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Problem with saving tables to datastores?

I'm trying to create an admin system with ranks etc and I'm having trouble with saving players that I admin along with their ranks. This is my first time using DataStores so please bear with me if I did anything retarded.

I save their admin and their ranks like this:

Module Script

adminModule.Database = DataStoreService:GetDataStore("AdminCommands")
adminModule.AdminList = {28356112} --Preset admin

adminModule.Ranks = {
	["28356112"] = {255, 150}, --Preset  ranks
	["29580606"] = {10} --10 isnt an admin thats why i dont have this userid in AdminList
}

adminModule.Prefix = "?"

local aList
local aRanks
pcall(function()
    aList = adminModule.Database:GetAsync("AdminList")
	aRanks = adminModule.Database:GetAsync("Ranks")
end)

if not aList then
    adminModule.Database:SetAsync("AdminList", adminModule.AdminList)
    aList = adminModule.AdminList
end

if not aRanks then
	adminModule.Database:SetAsync("Ranks", adminModule.Ranks)
	aRanks = adminModule.Ranks
end

--Saving admin
table.insert(aList, userid)
adminModule.Database:SetAsync("AdminList", aList)

--Saving rank
aRanks[tostring(userid)] = {rank} --im using tostring here because i get userid by using game.Players:GetUserIdFromNameAsync and I want to turn userid into the key name
adminModule.Database:SetAsync("Ranks", aRanks)

For some reason, when I use a command that gets all the admins, I sometimes find multiple instances of the same admin who apparently has no rank (if the player has a rank then it would show in the [ ] in front of their name), and apparently has no admin when it clearly shows the player does in the GUI that lists the admins.

https://gyazo.com/65483b40d639d465ee47720b14e1b835

In the GUI I get the admins of the game by using

--Local Script (FireClient from the module)
for i,v in pairs(aList) do
	--Text Label creation stuff
	for a = 1,#game.Players:GetNameFromUserIdAsync(v) do
		--adminName is just a textlabel parented to the UI
		adminName.Text = string.sub(game.Players:GetNameFromUserIdAsync(v), 1, a) 
		wait(0.05)
	end

1 answer

Click for Explorer setup

ServerScriptService

Script: Controls all the data

local AdminModule = require( game:GetService('ServerStorage'):WaitForChild('AdminsModule') )
local AdminsList = AdminModule.SavedAdminList

local players = game:GetService('Players')
players.PlayerAdded:Connect(function(player)
	AdminModule:Save(player, {1337, 7331})
	AdminModule:PrintAllAdmins()
	--AdminModule:Delete(player)
end)

ServerStorage

AdminsModule: Holds all the data and functions

local DataStoreService = game:GetService('DataStoreService')

local DefaultRank = 150
local basekey = '%s_%d'
local adminModule = { Prefix = "?" }
adminModule.AdminCommands = DataStoreService:GetDataStore("AdminCommands")

adminModule.SavedAdmins = DataStoreService:GetDataStore("AdminList") 
adminModule.Admins = {
	{ Name = 'TestSubject', userid = 1234, ranks = {255, 150} }	
}

-- Add SavedAdmins to the Preset 'Admins' table 
for i,v in pairs(adminModule.SavedAdmins:GetChildren()) do 
	table.insert(adminModule.Admins, v)
end  

function adminModule:PrintAllAdmins()
	for i,v in pairs(adminModule.Admins) do 
		print ( ('%s: %s'):format(v.Name, table.concat(v.ranks, ', ')) )
	end
end

-- Returns the Player key for the DataStore
function adminModule:ReturnKey(player)
	return basekey:format(player.Name, player.UserId)
end

-- Gets PlayerData From Admins
function adminModule:GetPlayerDataFromTable(player)
	for i,v in pairs(self.Admins) do 
		if v.userid == player.UserId then 
			return v, i
		end
	end
	return false
end

function adminModule:Save(player, ranks)
	local key = self:ReturnKey(player)
	if key then 
		-- wrap DataServices in a pcall()
		local success, problem = pcall(function()
			-- Make our data table
			local this = self.SavedAdmins:GetAsync(key) or {}
			-- Make our keys
			this.ranks = ranks or {DefaultRank}
			this.userid = player.UserId
			this.Name = player.Name
			-- Save our table into Admins
			table.insert (self.Admins, this)
			-- Save our data 
			return self.SavedAdmins:SetAsync(key, this)
		end)
		if not success then 
			print ( problem )
		end
	end
end


function adminModule:Delete(player)
	-- Are we even an admin?
	local SavedAdmin, Index = self:GetPlayerDataFromTable(player)
	local key = SavedAdmin and self:ReturnKey(player) or false
	if SavedAdmin then 
		local success, problem = pcall(function()
			-- Remove our data from 
			return self.SavedAdmins:RemoveAsync( key )
		end)
		if not success then 
			print ( problem )
		end
		-- remove our data from Admins
		table.remove(self.Admins, Index )
	else
		print ( ('%s is not an admin!'):format(player.Name) ) 
	end
end


return adminModule

StarterCharacterScripts

  • LocalScript: Displays all the data
    • Script: Gets all data from the server
    • RemoteFunction
    • ScreenGUI

LocalScript

local GetAdmins = script:WaitForChild('GetAdmins'):InvokeServer()
local ScreenGui = script:WaitForChild('ScreenGui')
local Frame = ScreenGui:WaitForChild("Frame")


for i,v in pairs(GetAdmins) do 
	local TextButton = Instance.new("TextButton")
	TextButton.Size = UDim2.new(1,0,0,30)
	TextButton.Text = ('%s: %s'):format(v.Name, table.concat(v.ranks, ', '))
	TextButton.Position = UDim2.new(0,0,0, (i-1) * 30 ) 
	TextButton.Parent = Frame
end

ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")

Script

local GetAdmins = Instance.new('RemoteFunction')
GetAdmins.Name = 'GetAdmins'

local AdminsModule = require ( game:GetService('ServerStorage'):WaitForChild("AdminsModule") )

function GetAdmins.OnServerInvoke(player)
	return AdminsModule.Admins
end

GetAdmins.Parent = script.Parent

3 comments

0

Thanks for taking your time to answer my question, but when I joined my game to let the DataStores save my admin, leave, and remove the save function from the PlayerAdded function. It turns out that the admin doesn't save, I don't want to constantly giving people admin each time they join, I want to just admin them once and save their admin.

0

also what is basekey?

0

Move the save method wherever you need it. Basekey is the data key, it formats to azarth_useridhere.

This discussion is preserved as it was. New answers, comments, and votes are closed.