Is it bad to have multiple datastores?

you can create a table with players in the game

local PlayerSession = {
   -- [Player.UserId]
}

every time a player joins, try to see if the player has data, if the player has data, set the key in PlayerSession to the player’s userid (player.UserId), if not then return the template

local serialized_data = ShikaiUnlockedData:GetAsync(player.UserId)

if serialized_data then
  -- load the player data in
   PlayerSession[player.UserId] = serialized_data
else
  -- first time? return templete
   PlayerSession[player.UserId] = ReturnTemplete()
end

then after, you’d set the data

local Data = PlayerSession[player.UserId]
ShikaiName.Value = Data.ShikaiName

make sure you also wrap datastore calls in a pcall, they can fail

local Ok,Data = pcall(function()
      return ShikaiUnlockedData:GetAsync(player.UserId)
end)

if Ok then
   -- Success 

else 
   -- Fail
end

When I put this into my code on line 113

	ShikaiName.Value = Data.ShikaiName

I get the error:

can i see the current code? did the datastore load correctly?

local HttpService = game:GetService("HttpService")

local DataStore = game:GetService("DataStoreService")

local ShikaiUnlockedData = DataStore:GetDataStore("ShikaiUnlockedData")

local Data = {
	["Unlocked"] = "NotUnlocked",
	["Call"] = "Locked",
	["ShikaiName"] = "Locked",
	["WhatSword"] = "None",
}

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Folder",player)
	leader.Name = "Data"

local function ReturnTemplete()
		return  {
			["Unlocked"] = "NotUnlocked",
			["Call"] = "Locked",
			["ShikaiName"] = "Locked",
			["WhatSword"] = "None",
		}
	end

local Folder = Instance.new("Folder",leader)
	Folder.Name = "ShikaiInfo"
	
	
	local ShikaiName = Instance.new("StringValue",Folder)
	ShikaiName.Name = "ShikaiName"
	
	

	
	local PlayerSession = {
		-- [Player.UserId]
	}

	
	
	
	
	local serialized_data = ShikaiUnlockedData:GetAsync(player.UserId)
	
	if serialized_data then
		-- load the player data in
		PlayerSession[player.UserId] = serialized_data
	else
		-- first time? return templete
		PlayerSession[player.UserId] = ReturnTemplete()
	end

	
	local Data = PlayerSession[player.UserId]
	
	
	
	
	ShikaiName.Value = Data.ShikaiName
	
	
	ShikaiName.Changed:Connect(function()
		Data.ShikaiName = ShikaiName.Value
		warn(Data)
		local serialized = HttpService:JSONEncode(Data)
		ShikaiUnlockedData:SetAsync(player.UserId,serialized)
		print("SAVED")
	end)




end)

forgot to decode the data

local HttpService = game:GetService("HttpService")

local DataStore = game:GetService("DataStoreService")

local ShikaiUnlockedData = DataStore:GetDataStore("ShikaiUnlockedData")

local function ReturnTemplete()
	return  {
		["Unlocked"] = "NotUnlocked",
		["Call"] = "Locked",
		["ShikaiName"] = "Locked",
		["WhatSword"] = "None",
	}
end

local PlayerSession = {
	-- [Player.UserId]
}

game.Players.PlayerAdded:connect(function(player)
	
	local leader = Instance.new("Folder",player)
	leader.Name = "Data"

	local Folder = Instance.new("Folder",leader)
	Folder.Name = "ShikaiInfo"

	local ShikaiName = Instance.new("StringValue",Folder)
	ShikaiName.Name = "ShikaiName"

	local serialized_data = ShikaiUnlockedData:GetAsync(player.UserId)

	if serialized_data then
		-- load the player data in
		serialized_data = HttpService:JSONDecode(serialized_data)
		
		PlayerSession[player.UserId] = serialized_data
	else
		-- first time? return templete
		PlayerSession[player.UserId] = ReturnTemplete()
	end


	local Data = PlayerSession[player.UserId]
	ShikaiName.Value = Data.ShikaiName


	ShikaiName.Changed:Connect(function()
		Data.ShikaiName = ShikaiName.Value
		warn(Data)
		local serialized = HttpService:JSONEncode(Data)
		ShikaiUnlockedData:SetAsync(player.UserId,serialized)
		print("SAVED")
	end)

end)
1 Like

Thank you so much for dealing with me I appreciate it a lot

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.