DataStores issues with boolValue

Hi! I am trying to store several bool values in datastores, the console (in-game player) shows me an error, I would like to know what is wrong and correct it “Keeping my way of programming” in this way will support me for learning.

Could someone give me a hand? I would really appreciate it.

ServerScript:

local DataStoreService = game:GetService('DataStoreService')
local role_Assignment_DataStores = DataStoreService:GetDataStore('role_Assignment_DataStores')


game.Players.PlayerAdded:Connect(function(player)
	local privileges = Instance.new('Folder', player)
	privileges.Name = 'privileges' 

	local Command_Access = 
		{
			master = Instance.new("BoolValue", privileges),
			team_Leader = Instance.new("BoolValue", privileges),
			process_Leader = Instance.new("BoolValue", privileges),
			member = Instance.new("BoolValue", privileges),
			auditor = Instance.new("BoolValue", privileges)
		}
	
	Command_Access.master.Name = "master"
	Command_Access.team_Leader.Name = "team_Leader"
	Command_Access.process_Leader.Name = "process_Leader"
	Command_Access.member.Name = "member"
	Command_Access.auditor.Name = "auditor"

	local Command_Access_Data

	local success, errormessage = pcall(function()
		Command_Access_Data = role_Assignment_DataStores:GetAsync('master_'..player.UserId)
		Command_Access_Data = role_Assignment_DataStores:GetAsync('team_Leader_'..player.UserId)
		Command_Access_Data = role_Assignment_DataStores:GetAsync('process_Leader_'..player.UserId)
		Command_Access_Data = role_Assignment_DataStores:GetAsync('member_'..player.UserId)
		Command_Access_Data = role_Assignment_DataStores:GetAsync('auditor_'..player.UserId)
	end)

	if Command_Access_Data and success then
		print("Checking command access data...")
		player.privileges.master.Value = Command_Access_Data
		player.privileges.team_Leader.Value = Command_Access_Data
		player.privileges.process_Leader.Value = Command_Access_Data
		player.privileges.member.Value = Command_Access_Data
		player.privileges.auditor.Value = Command_Access_Data
		print("Command_Access_Data is saved!!")
	else
		warn(errormessage)
		print("Command_Access_Data FAIL!! Something is wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local set_master = player.privileges.master.Value
	local set_team_Leader = player.privileges.team_Leader.Value
	local set_process_Leader = player.privileges.process_Leader.Value
	local set_member = player.privileges.member.Value
	local set_auditor = player.privileges.auditor.Value
	
	local success, errormessage = pcall(function()
		role_Assignment_DataStores:SetAsync("master_" .. player.UserId, set_master)
		role_Assignment_DataStores:SetAsync("team_Leader_" .. player.UserId, set_team_Leader)
		role_Assignment_DataStores:SetAsync("process_Leader_" .. player.UserId, set_process_Leader)
		role_Assignment_DataStores:SetAsync("member_" .. player.UserId, set_member)
		role_Assignment_DataStores:SetAsync("auditor_" .. player.UserId, set_auditor)
	end)
	
end)

Your pcall is going to return nil every single time which assigns nil to the Command_Access_Data variable.

I recommend having a single datastore which contains the rank, it’s going to make things way simpler and won’t overload your datastore queue.

and why does this happen? There’s no way to fix it? :frowning:

You could have a large strand of elseifs (local rank1 = player.privileges.master if not rank1 then do rank2, rank3)

I would like to keep everything in a dictionary, is that not possible? :scream: :frowning:

Why not have the entirety of the player’s data (including access data) in a single dictionary?

But the way I have it created, there is no way to fix it? :frowning:

You can migrate the old data to a new data structure, you can also just make 5 different variables and assign the value to them.

your mean something like this?

Remember tables? You can use them to store multiple values inside arrays & dictionaries

DataStores are capable of saving tables as well, as long as you reference their values correctly

local DataStoreService = game:GetService('DataStoreService')
local role_Assignment_DataStores = DataStoreService:GetDataStore('role_Assignment_DataStores')

game.Players.PlayerAdded:Connect(function(player)
	local privileges = Instance.new('Folder', player)
	privileges.Name = 'privileges' 

	local Command_Access = 
		{
			master = Instance.new("BoolValue", privileges),
			team_Leader = Instance.new("BoolValue", privileges),
			process_Leader = Instance.new("BoolValue", privileges),
			member = Instance.new("BoolValue", privileges),
			auditor = Instance.new("BoolValue", privileges)
		}

	Command_Access.master.Name = "master"
	Command_Access.team_Leader.Name = "team_Leader"
	Command_Access.process_Leader.Name = "process_Leader"
	Command_Access.member.Name = "member"
	Command_Access.auditor.Name = "auditor"

	local Command_Access_Data

	local success, errormessage = pcall(function()
		Command_Access_Data = role_Assignment_DataStores:GetAsync('User_'..player.UserId)
	end)

	if Command_Access_Data and success then
		print("Checking command access data...")
		player.privileges.master.Value = Command_Access_Data.Master
		player.privileges.team_Leader.Value = Command_Access_Data.Team_Leader
		player.privileges.process_Leader.Value = Command_Access_Data.Process_Leader
		player.privileges.member.Value = Command_Access_Data.Member
		player.privileges.auditor.Value = Command_Access_Data.Auditor
		print("Command_Access_Data is loaded!!")
	else
		warn(errormessage)
		print("Command_Access_Data FAIL!! Something is wrong.")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local DataToSave = {
		Master = player.privileges.master.Value;
		Team_Leader = player.privileges.team_Leader.Value;
		Process_Leader = player.privileges.process_Leader.Value;
		Member = player.privileges.member.Value;
		AUditor = player.privileges.auditor.Value
	}

	local success, errormessage = pcall(function()
		role_Assignment_DataStores:SetAsync('User_'..player.UserId, DataToSave)
	end)
	
	if success then
		print("Data saved!")
	else
		warn(errormessage)
	end
end)

Try this perhaps?

1 Like

Not sure what you mean.

I assume you have more than just the command data, just merge the command access data with the player’s normal data.

local playerData = {
    ['Money'] = 100;
    -- etc
    ['CommandAccessData'] = {
        ['Master'] = true;
        ['TeamLeader'] = false;
        ['ProcessLeader'] = false;
        -- etc
    }
}
1 Like

Oh I just saw this work! the server had not been updated. :smiley:

Thank you guys very much for your help! :smiley: :smiley:

1 Like

I have a question … why do you use: User_ in the getter and the setter?

It’s mainly just for easier use & personal preference really, if you wanted to print out that specific key of the Player it’d return back something like:

User_2309294

So we know that what we’re saving is a Key of a Player’s Stats, it’s less confusing than rather having to do:

2309294

Which would just return back the UserId & not the User_

Either way would work really, but I just put User_ for simpler use :wink:

1 Like

Thank you very much for the clarification! :smiley:

1 Like