My data stores isn't loading the correct things

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    so um my data stores are saving perfectly, it saves all the correct data and stuff. BUT when loading it loads some wrong values

  2. What is the issue? Include screenshots / videos if possible!
    It’s loading old Data for some reason and on rare occasions, it wouldn’t save

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve been solo on this whole thing, this was working like 10 minutes ago and now it’s not, I’ve been using the dev forums for answers.


(the J stands for jetpack and the R stands for Radio)

local Players, DataStoreService = game:GetService("Players"), game:GetService("DataStoreService")
local ColorStorage = DataStoreService:GetDataStore("ColorManager")

local function load(player: Player)
	local PlayerUserId = 'Player_'.. player.UserId
	local Data = ColorStorage:GetAsync(PlayerUserId)
	if Data then
		local remoteEvent = player.Parent.Parent.StarterPlayer.StarterCharacterScripts.Radio.ServerToclient
		local remoteEvent2 = player.Parent.Parent.StarterPlayer.StarterCharacterScripts.JetPack.ServerToclient
		print("load Data")
		if Data['PrimaryColorJ'] ~= nil then
			local Jcolor1 = Color3.new(unpack(Data['PrimaryColorJ']))
			local Jcolor2 =  Color3.new(unpack(Data['SecondaryColorJ']))
			local Jcolor3 = Color3.new(unpack(Data['ThirdColorJ']))
			print(Data)
			remoteEvent2:FireClient(player, Jcolor1, Jcolor2, Jcolor3)
		end
		if Data['PrimaryColorR'] ~= nil then
			local Rcolor1 = Color3.new(unpack(Data['PrimaryColorR']))
			local Rcolor2 =  Color3.new(unpack(Data['SecondaryColorR']))
			remoteEvent:FireClient(player, Rcolor1, Rcolor2)
		end
	else
	end
end

local function colortable(player)
	local colorsTable = {}
	local Found1 = Players:WaitForChild(player.name).Backpack:FindFirstChild("JetPack")
	local Found2 = Players:WaitForChild(player.name).Backpack:FindFirstChild("BoomBox")
	if Found1 and Found2 then
		for _, ColorValues in pairs(Found1.Jetpack.MainParts:GetChildren()) do
			colorsTable[ColorValues.Name] = tostring(ColorValues.Color):split(", ")
			print(tostring(ColorValues.Color):split(", "))
		end
		for _, ColorValues in pairs(Found2.BoomBox.MainParts:GetChildren()) do
			colorsTable[ColorValues.Name] = tostring(ColorValues.Color):split(", ")
			print(tostring(ColorValues.Color):split(", "))
		end
		return colorsTable
	end
end

local function save(player: Player)	
	print("SavingData")
	local colorsaved = colortable(player)
	local success, errormessage = pcall(function()
		local PlayerUserId = 'Player_'.. player.UserId
		print("DataSaved")
		ColorStorage:SetAsync(PlayerUserId, colorsaved)
	end)
end

Players.PlayerAdded:Connect(function(player: Player)
	load(player)
end)

Players.PlayerRemoving:Connect(function(player: Player)
	player.CharacterRemoving:Connect(function(Character)
		local Check = Players:GetPlayerFromCharacter(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
		print("PlayerLeftSaveNow")
		save(player)
	end)
end)

PlayerRemoving is not enough when it comes to datastores!
You need to also use BindToClose to make sure the last player who leaves the server has their data saved.

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

1 Like

well, that’s one of my problems solved. but what about when it comes to loading data, it’s not loading the values i want it to load (unless i’ve miss read what you said)

That would be because it didnt save the previous data wouldnt it?

Unless you mean loading them incorrectly in the wrong RGB order, in which case the problem is simple
you’re not loading them in the correct order;
try to not use unpack to enter the RGB values, use [1],[2],[3] instead to make sure its in the correct order

1 Like


it’s saving correctly it even saved the jetpack colors from the last time since i haven’t touched those values, i don’t know what it’s loading but it keeps loading the same old data. it doesn’t get overwritten, when i print out the load data it just shows the same old data, and the saved data is the right data i want. I’m trying to think what caused this issue since this started to have problems out of nowhere

try it with the bindtocloase, see if it saves and then loads the correct data

1 Like

yeah the bindtoclose made it work, just doing a few more test atm before i mark you for a solutions (alright everything is working thanks for the help) … ( yeah I’ve noticed there’s a bit of delay which was kinda annoying)

1 Like

personally i recommend checking if you’re in studio using RunService:IsStudio() for better quality of life (if you noticed using bindtoclose makes it wait a long time before actually ending the test session)