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

What's the best way to save multiple values of multiple players to a key in a DataStore?

So, I've been working on an admin command list recently and I was confused on how to make everyone's data about admins and bans similar to everyone else's data.

I was told to use a Key instead of using a Player's UserId, and I have tried doing this. The one problem is, when you save values to a DataStore with a key, it'll save in 2 columns and 1 row. 1 Column is the Key, and the other is all the data.

I was thinking of saving the data using a string as in

"Player_Id Not_Admin Not_Banned"

or

"Player_Id Admin Not_Banned"

or

"Player_Id Not_Admin Banned"

Just to indicate if they are banned or are admins.

Then it would save as multiple strings, and I could use a for loop.

for _,val in pairs(data) do
	if string.match(player.UserId, val) then
		print("Existing Data")
	else
		print("Creating New Data")
	end
end

Now, I thought this should work, but it seemed as the game didn't load nor save the data in live servers and studio.

So, now I'm confused on how to fix this.

Here is the full code.

ServerScript in ServerScriptService

local DSS = game:GetService("DataStoreService")
local GDS = DSS:GetDataStore("_admin_bans_")
local Key = "08ufndasyc79cjfomauihcfdn7af3uhgrieavnfiu"

game.Players.PlayerAdded:Connect(function(player)
	local dataFolder = Instance.new("Folder")
	dataFolder.Name = "DataFolder"
	dataFolder.Parent = player
	
	local admin = Instance.new("Folder")
	admin.Name = "Admin"
	admin.Parent = dataFolder
	
	local banned = Instance.new("Folder")
	banned.Name = "Banned"
	banned.Parent = dataFolder
	
	local data
	local success, err = pcall(function()
		data = GDS:GetAsync(Key)
	end)
	
	if success then
		if type(data) == "table" then
			for _,val in pairs(data) do
				if string.match(player.UserId, val) then
					local args = string.split(val, " ")
					if args[2] == "Admin" then
						admin.Value = true
					elseif args[2] == "Not_Admin" then
						admin.Value= false
					else
						admin.Value = false
					end
					
					if args[3] == "Banned" then
						banned.Value = true
					elseif args [3] == "Not_Banned" then
						banned.Value = false
					else
						banned.Value = false
					end
					
					print("Loaded Data")
				end
			end
		end
	else
		warn("Error Loading Data\n"..err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local dataFolder = player:WaitForChild("DataFolder")
	local admin = dataFolder:WaitForChild("Admin")
	local banned = dataFolder:WaitForChild("Banned")
	
	local dataToSave
	
	if admin.Value == true then
		if banned.Value == true then
			dataToSave = player.UserId.. " Admin Banned"
		else
			dataToSave = player.UserId.. " Admin Not_Banned"
		end
	elseif admin.Value == false then
		if banned.Value == false then
			dataToSave = player.UserId.. " Not_Admin Not_Banned"
		else
			dataToSave = player.UserId.. " Not_Admin Banned"
		end
	end
	
	local success, err = pcall(function()
		GDS:SetAsync(Key, dataToSave)
	end)
	
	if success then
		print("Success")
	else
		warn("Error Saving Data\n"..err)
	end
end)

game:BindToClose(function()
	for _,player in pairs(game:GetService("Players"):GetPlayers()) do
		local dataFolder = player:WaitForChild("DataFolder")
		local admin = dataFolder:WaitForChild("Admin")
		local banned = dataFolder:WaitForChild("Banned")
		
		local dataToSave
		
		if admin.Value == true then
			if banned.Value == true then
				dataToSave = player.UserId.. " Admin Banned"
			else
				dataToSave = player.UserId.. " Admin Not_Banned"
			end
		elseif admin.Value == false then
			if banned.Value == false then
				dataToSave = player.UserId.. " Not_Admin Not_Banned"
			else
				dataToSave = player.UserId.. " Not_Admin Banned"
			end
		end
		
		local success, err = pcall(function()
			GDS:SetAsync(Key, dataToSave)
		end)
		
		if success then
			print("Success")
		else
			warn("Error Saving Data\n"..err)
		end
	end
end)

If you need anymore information I'll be glad to edit this post. Any help is appreciated.

2 comments

0

Just for clarification, what exactly is the data you are trying to save? Is there two fields, players who are banned and players who have admin priveleges?

0

Indeed, I was thinking of saving their data using a string formatted with their name and the data thats being saved ^, and using a for loop to match the players name with the strings name

1 answer

✓ Accepted answer

If I'm understanding what you're trying to do properly, you can accomplish this much easier using tables as opposed to strings. Simply have the value be a dictionary with two keys:

local adminBans= {
	Banned = {12345, 67890}, --One table holds the UserIDs of those banned
	Admins = {12345, 67890} --The other table holds the UserIDs of those with admin
}

This way you'd have all your values neatly nested in this dictionary, and can see if a player should be banned/given admin based on whether or not their UserID can be found in either of these tables.

local adminBans = GDS:GetAsync(Key)
if table.find(adminBans.Banned, player.UserId) then
	--Player is banned
end
if table.find(adminBans.Admins, player.UserId) then
	--Player has admin
end

--

Although I'm not sure of how you're using this, I don't see the need for everyone to have access to the entire list of banned players and admins. Would it not be simpler to go back to saving data per player (Using their UserID as a key again) but setting the value only to whether or not that specific player is banned or has admin? So each player would save a dictionary like so:

local adminBans = {
	isBanned = false,
	isAdmin = true
}

Again, there may be a reason I'm not aware of that you'd like to have access to the entire list of banned/admins so I'll just leave this here.

3 comments

0

So, you're recommending that I save if they're admin or banned and load that data into BoolValues and if they're admin they get admin if they're banned they get kicked?

0

Yeah. Unless you need it for something, there's no reason every players data needs to be stored together. Each player can just handle their own data like that.

1

Ah ok, thanks :)

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