Need help with DataStores

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!
    I have rewritten my DataStores multiple times after they had randomly stopped working. I cannot seem to get them to work anymore.

  2. What is the issue? Include screenshots / videos if possible!
    The DataStore is NOT saving any of my values.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have looked at multiple Developer Forum posts and I have implemented and tried most of the solutions but I have not been able to get them to work for me.

This is probably my third time rewriting it, at first I had 3 separate datastores for stats, cosmetics, and the inventory then it stopped working so I tried rewriting each DataStore and that didn’t work, so I tried making one with all of them inside of a singular DataStore and no matter what I try I can’t seem to get it to work. All suggestions and help is appreciated.

Terrible Code below:

local ds = game:GetService("DataStoreService"):GetDataStore("EverythingObbyRace")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name="leaderstats"
	local cosmetics = Instance.new("Folder",player)
	cosmetics.Name="Cosmetics"
	local bools = Instance.new("Folder",player)
	bools.Name="Bools"
	--leaderstats
	local cash = Instance.new("IntValue",leaderstats)
	cash.Name = "Cash"
	local gems = Instance.new("IntValue",leaderstats)
	gems.Name = "Gems"
	local wins = Instance.new("IntValue",leaderstats)
	wins.Name = "Wins"
	--cosmetics
	local currentTitle = Instance.new("StringValue",cosmetics)
	currentTitle.Name = "currentTitle"
	local currentPet = Instance.new("StringValue",cosmetics)
	currentPet.Name = "currentPet"
	local currentEffect = Instance.new("StringValue",cosmetics)
	currentEffect.Name = "currentEffect"
	--ITEM LIST
	--Titles
	local item1 = Instance.new("BoolValue",bools)
	item1.Name = "Beginner"
	local item2 = Instance.new("BoolValue",bools)
	item2.Name = "Star"
	local item3 = Instance.new("BoolValue",bools)
	item3.Name = "Rookie"
	local item4 = Instance.new("BoolValue",bools)
	item4.Name = "Veteran"
	local item5 = Instance.new("BoolValue",bools)
	item5.Name = "VIP"
	local item6 = Instance.new("BoolValue",bools)
	item6.Name = "Skilled"
	local item7 = Instance.new("BoolValue",bools)
	item7.Name = "Pro"
	local item8 = Instance.new("BoolValue",bools)
	item8.Name = "Master"
	local item9 = Instance.new("BoolValue",bools)
	item9.Name = "Expert"
	local item10 = Instance.new("BoolValue",bools)
	item10.Name = "Game Creator"
	local item11 = Instance.new("BoolValue",bools)
	item11.Name = "Obby God"
	local item12 = Instance.new("BoolValue",bools)
	item12.Name = "dan"
	--eFFECTs
	local item13 = Instance.new("BoolValue",bools)
	item13.Name = "Classic Fire"
	local item14 = Instance.new("BoolValue",bools)
	item14.Name = "No Effect"
	local item15 = Instance.new("BoolValue",bools)
	item15.Name = "VIP Fire"
	local item16 = Instance.new("BoolValue",bools)
	item16.Name = "Cosmic Flames"
	local item17 = Instance.new("BoolValue",bools)
	item17.Name = "Rainbow Flames"
	--Pets
	local item18 = Instance.new("BoolValue",bools)
	item18.Name = "No Pet"
	local item19 = Instance.new("BoolValue",bools)
	item19.Name = "Ghost"
	local item20 = Instance.new("BoolValue",bools)
	item20.Name = "Lizard"
	local item21 = Instance.new("BoolValue",bools)
	item21.Name = "Apple"
	local item22 = Instance.new("BoolValue",bools)
	item22.Name = "Banana"
	local item23 = Instance.new("BoolValue",bools)
	item23.Name = "Cousin Tentacles"
	local item24 = Instance.new("BoolValue",bools)
	item24.Name = "Dr. Ishmael"
	local item25 = Instance.new("BoolValue",bools)
	item25.Name = "Mr. Tentacles"
	local item26 = Instance.new("BoolValue",bools)
	item26.Name = "Mrs. Tentacles"
	local item27 = Instance.new("BoolValue",bools)
	item27.Name = "Rainbow Squid"
	local item28 = Instance.new("BoolValue",bools)
	item28.Name = "Staring Cat"
	local item29 = Instance.new("BoolValue",bools)
	item29.Name = "Turtle"
	local item30 = Instance.new("BoolValue",bools)
	item30.Name = "Pop Cat"
	local item31 = Instance.new("BoolValue",bools)
	item31.Name = "Panda"
	local item32 = Instance.new("BoolValue",bools)
	item32.Name = "Fish"
	local item33 = Instance.new("BoolValue",bools)
	item33.Name = "Duck"
	
	local stats = ds:GetAsync(player.UserId)
	
	if stats ~= nil then
		cash.Value = stats[1]
		gems.Value = stats[2]
		wins.Value = stats[3]
		
		currentTitle.Value = stats[4]
		currentPet.Value = stats[5]
		currentEffect.Value = stats[6]
		
		item1.Value = stats[7]
		item2.Value = stats[8]
		item3.Value = stats[9]
		item4.Value = stats[10]
		item5.Value = stats[11]
		item6.Value = stats[12]
		item7.Value = stats[13]
		item8.Value = stats[14]
		item9.Value = stats[15]
		item10.Value = stats[16]
		item11.Value = stats[17]
		item12.Value = stats[18]
		item13.Value = stats[19]
		item14.Value = stats[20]
		item15.Value = stats[21]
		item16.Value = stats[22]
		item17.Value = stats[23]
		item18.Value = stats[24]
		item19.Value = stats[25]
		item20.Value = stats[26]
		item21.Value = stats[27]
		item22.Value = stats[28]
		item23.Value = stats[29]
		item24.Value = stats[30]
		item25.Value = stats[31]
		item26.Value = stats[32]
		item27.Value = stats[33]
		item28.Value = stats[34]
		item29.Value = stats[35]
		item30.Value = stats[36]
		item31.Value = stats[37]
		item32.Value = stats[38]
		item33.Value = stats[39]
	else
		cash.Value = 250
		gems.Value = 0
		wins.Value = 0

		currentTitle.Value = "Beginner"
		currentPet.Value = "No Pet"
		currentEffect.Value = "No Effect"

		item1.Value = true
		item2.Value = false
		item3.Value = false
		item4.Value = false
		item5.Value = false
		item6.Value = false
		item7.Value = false
		item8.Value = false
		item9.Value = false
		item10.Value = false
		item11.Value = false
		item12.Value = false
		item13.Value = false
		item14.Value = true
		item15.Value = false
		item16.Value = false
		item17.Value = false
		item18.Value = true
		item19.Value = false
		item20.Value = false
		item21.Value = false
		item22.Value = false
		item23.Value = false
		item24.Value = false
		item25.Value = false
		item26.Value = false
		item27.Value = false
		item28.Value = false
		item29.Value = false
		item30.Value = false
		item31.Value = false
		item32.Value = false
		item33.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local save = {}
	
	table.insert(save, player.leaderstats.Cash.Value)
	table.insert(save, player.leaderstats.Gems.Value)
	table.insert(save, player.leaderstats.Wins.Value)
	
	table.insert(save, player.Cosmetics.currentTitle)
	table.insert(save, player.Cosmetics.currentPet)
	table.insert(save, player.Cosmetics.currentEffect)
	--TITLES SECTION 1
	table.insert(save, player.Bools.Beginner.Value)
	table.insert(save, player.Bools.Star.Value)
	table.insert(save, player.Bools.Rookie.Value)
	table.insert(save, player.Bools.Veteran.Value)
	table.insert(save, player.Bools.VIP.Value)
	table.insert(save, player.Bools.Skilled.Value)
	table.insert(save, player.Bools.Pro.Value)
	table.insert(save, player.Bools.Master.Value)
	table.insert(save, player.Bools.Expert.Value)
	table.insert(save, player.Bools["Game Creator"].Value)
	table.insert(save, player.Bools["Obby God"].Value)
	table.insert(save, player.Bools.dan.Value)
	--EFFECTS SECTION 1
	table.insert(save, player.Bools["Classic Fire"].Value)
	table.insert(save, player.Bools["No Effect"].Value)
	table.insert(save, player.Bools["VIP Fire"].Value)
	table.insert(save, player.Bools["Cosmic Flames"].Value)
	table.insert(save, player.Bools["Rainbow Flames"].Value)
	--PETS SECTION 1
	table.insert(save, player.Bools["No Pet"].Value)
	table.insert(save, player.Bools["Ghost"].Value)
	table.insert(save, player.Bools["Lizard"].Value)
	table.insert(save, player.Bools["Apple"].Value)
	table.insert(save, player.Bools["Banana"].Value)
	table.insert(save, player.Bools["Cousin Tentacles"].Value)
	table.insert(save, player.Bools["Dr. Ishmael"].Value)
	table.insert(save, player.Bools["Mr. Tentacles"].Value)
	table.insert(save, player.Bools["Mrs. Tentacles"].Value)
	table.insert(save, player.Bools["Rainbow Squid"].Value)
	table.insert(save, player.Bools["Staring Cat"].Value)
	table.insert(save, player.Bools["Turtle"].Value)
	table.insert(save, player.Bools["Pop Cat"].Value)
	table.insert(save, player.Bools["Panda"].Value)
	table.insert(save, player.Bools["Fish"].Value)
	table.insert(save, player.Bools["Duck"].Value)
	
	ds:SetAsync(player.UserId, save)
end)

There was a similar issue to yours I believe, only if you’re testing this in studio, try this solution

Are there any errors?

Sidenote: you should pcall your async requests so it doesnt shut down your script if robllox datastores fail.

I am not testing it in studio.

I’ll do that now and see what the error is. I’m pretty sure it just isn’t saving.

Add a bind to close function,

local ds = game:GetService("DataStoreService"):GetDataStore("EverythingObbyRace")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name="leaderstats"
	local cosmetics = Instance.new("Folder",player)
	cosmetics.Name="Cosmetics"
	local bools = Instance.new("Folder",player)
	bools.Name="Bools"
	--leaderstats
	local cash = Instance.new("IntValue",leaderstats)
	cash.Name = "Cash"
	local gems = Instance.new("IntValue",leaderstats)
	gems.Name = "Gems"
	local wins = Instance.new("IntValue",leaderstats)
	wins.Name = "Wins"
	--cosmetics
	local currentTitle = Instance.new("StringValue",cosmetics)
	currentTitle.Name = "currentTitle"
	local currentPet = Instance.new("StringValue",cosmetics)
	currentPet.Name = "currentPet"
	local currentEffect = Instance.new("StringValue",cosmetics)
	currentEffect.Name = "currentEffect"
	--ITEM LIST
	--Titles
	local item1 = Instance.new("BoolValue",bools)
	item1.Name = "Beginner"
	local item2 = Instance.new("BoolValue",bools)
	item2.Name = "Star"
	local item3 = Instance.new("BoolValue",bools)
	item3.Name = "Rookie"
	local item4 = Instance.new("BoolValue",bools)
	item4.Name = "Veteran"
	local item5 = Instance.new("BoolValue",bools)
	item5.Name = "VIP"
	local item6 = Instance.new("BoolValue",bools)
	item6.Name = "Skilled"
	local item7 = Instance.new("BoolValue",bools)
	item7.Name = "Pro"
	local item8 = Instance.new("BoolValue",bools)
	item8.Name = "Master"
	local item9 = Instance.new("BoolValue",bools)
	item9.Name = "Expert"
	local item10 = Instance.new("BoolValue",bools)
	item10.Name = "Game Creator"
	local item11 = Instance.new("BoolValue",bools)
	item11.Name = "Obby God"
	local item12 = Instance.new("BoolValue",bools)
	item12.Name = "dan"
	--eFFECTs
	local item13 = Instance.new("BoolValue",bools)
	item13.Name = "Classic Fire"
	local item14 = Instance.new("BoolValue",bools)
	item14.Name = "No Effect"
	local item15 = Instance.new("BoolValue",bools)
	item15.Name = "VIP Fire"
	local item16 = Instance.new("BoolValue",bools)
	item16.Name = "Cosmic Flames"
	local item17 = Instance.new("BoolValue",bools)
	item17.Name = "Rainbow Flames"
	--Pets
	local item18 = Instance.new("BoolValue",bools)
	item18.Name = "No Pet"
	local item19 = Instance.new("BoolValue",bools)
	item19.Name = "Ghost"
	local item20 = Instance.new("BoolValue",bools)
	item20.Name = "Lizard"
	local item21 = Instance.new("BoolValue",bools)
	item21.Name = "Apple"
	local item22 = Instance.new("BoolValue",bools)
	item22.Name = "Banana"
	local item23 = Instance.new("BoolValue",bools)
	item23.Name = "Cousin Tentacles"
	local item24 = Instance.new("BoolValue",bools)
	item24.Name = "Dr. Ishmael"
	local item25 = Instance.new("BoolValue",bools)
	item25.Name = "Mr. Tentacles"
	local item26 = Instance.new("BoolValue",bools)
	item26.Name = "Mrs. Tentacles"
	local item27 = Instance.new("BoolValue",bools)
	item27.Name = "Rainbow Squid"
	local item28 = Instance.new("BoolValue",bools)
	item28.Name = "Staring Cat"
	local item29 = Instance.new("BoolValue",bools)
	item29.Name = "Turtle"
	local item30 = Instance.new("BoolValue",bools)
	item30.Name = "Pop Cat"
	local item31 = Instance.new("BoolValue",bools)
	item31.Name = "Panda"
	local item32 = Instance.new("BoolValue",bools)
	item32.Name = "Fish"
	local item33 = Instance.new("BoolValue",bools)
	item33.Name = "Duck"
	
	local stats = ds:GetAsync(player.UserId)
	
	if stats ~= nil then
		cash.Value = stats[1]
		gems.Value = stats[2]
		wins.Value = stats[3]
		
		currentTitle.Value = stats[4]
		currentPet.Value = stats[5]
		currentEffect.Value = stats[6]
		
		item1.Value = stats[7]
		item2.Value = stats[8]
		item3.Value = stats[9]
		item4.Value = stats[10]
		item5.Value = stats[11]
		item6.Value = stats[12]
		item7.Value = stats[13]
		item8.Value = stats[14]
		item9.Value = stats[15]
		item10.Value = stats[16]
		item11.Value = stats[17]
		item12.Value = stats[18]
		item13.Value = stats[19]
		item14.Value = stats[20]
		item15.Value = stats[21]
		item16.Value = stats[22]
		item17.Value = stats[23]
		item18.Value = stats[24]
		item19.Value = stats[25]
		item20.Value = stats[26]
		item21.Value = stats[27]
		item22.Value = stats[28]
		item23.Value = stats[29]
		item24.Value = stats[30]
		item25.Value = stats[31]
		item26.Value = stats[32]
		item27.Value = stats[33]
		item28.Value = stats[34]
		item29.Value = stats[35]
		item30.Value = stats[36]
		item31.Value = stats[37]
		item32.Value = stats[38]
		item33.Value = stats[39]
	else
		cash.Value = 250
		gems.Value = 0
		wins.Value = 0

		currentTitle.Value = "Beginner"
		currentPet.Value = "No Pet"
		currentEffect.Value = "No Effect"

		item1.Value = true
		item2.Value = false
		item3.Value = false
		item4.Value = false
		item5.Value = false
		item6.Value = false
		item7.Value = false
		item8.Value = false
		item9.Value = false
		item10.Value = false
		item11.Value = false
		item12.Value = false
		item13.Value = false
		item14.Value = true
		item15.Value = false
		item16.Value = false
		item17.Value = false
		item18.Value = true
		item19.Value = false
		item20.Value = false
		item21.Value = false
		item22.Value = false
		item23.Value = false
		item24.Value = false
		item25.Value = false
		item26.Value = false
		item27.Value = false
		item28.Value = false
		item29.Value = false
		item30.Value = false
		item31.Value = false
		item32.Value = false
		item33.Value = false
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local save = {}
	
	table.insert(save, player.leaderstats.Cash.Value)
	table.insert(save, player.leaderstats.Gems.Value)
	table.insert(save, player.leaderstats.Wins.Value)
	
	table.insert(save, player.Cosmetics.currentTitle)
	table.insert(save, player.Cosmetics.currentPet)
	table.insert(save, player.Cosmetics.currentEffect)
	--TITLES SECTION 1
	table.insert(save, player.Bools.Beginner.Value)
	table.insert(save, player.Bools.Star.Value)
	table.insert(save, player.Bools.Rookie.Value)
	table.insert(save, player.Bools.Veteran.Value)
	table.insert(save, player.Bools.VIP.Value)
	table.insert(save, player.Bools.Skilled.Value)
	table.insert(save, player.Bools.Pro.Value)
	table.insert(save, player.Bools.Master.Value)
	table.insert(save, player.Bools.Expert.Value)
	table.insert(save, player.Bools["Game Creator"].Value)
	table.insert(save, player.Bools["Obby God"].Value)
	table.insert(save, player.Bools.dan.Value)
	--EFFECTS SECTION 1
	table.insert(save, player.Bools["Classic Fire"].Value)
	table.insert(save, player.Bools["No Effect"].Value)
	table.insert(save, player.Bools["VIP Fire"].Value)
	table.insert(save, player.Bools["Cosmic Flames"].Value)
	table.insert(save, player.Bools["Rainbow Flames"].Value)
	--PETS SECTION 1
	table.insert(save, player.Bools["No Pet"].Value)
	table.insert(save, player.Bools["Ghost"].Value)
	table.insert(save, player.Bools["Lizard"].Value)
	table.insert(save, player.Bools["Apple"].Value)
	table.insert(save, player.Bools["Banana"].Value)
	table.insert(save, player.Bools["Cousin Tentacles"].Value)
	table.insert(save, player.Bools["Dr. Ishmael"].Value)
	table.insert(save, player.Bools["Mr. Tentacles"].Value)
	table.insert(save, player.Bools["Mrs. Tentacles"].Value)
	table.insert(save, player.Bools["Rainbow Squid"].Value)
	table.insert(save, player.Bools["Staring Cat"].Value)
	table.insert(save, player.Bools["Turtle"].Value)
	table.insert(save, player.Bools["Pop Cat"].Value)
	table.insert(save, player.Bools["Panda"].Value)
	table.insert(save, player.Bools["Fish"].Value)
	table.insert(save, player.Bools["Duck"].Value)
	
	ds:SetAsync(player.UserId, save)


end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		coroutine.wrap(function()
			local save = {}

			table.insert(save, player.leaderstats.Cash.Value)
			table.insert(save, player.leaderstats.Gems.Value)
			table.insert(save, player.leaderstats.Wins.Value)

			table.insert(save, player.Cosmetics.currentTitle)
			table.insert(save, player.Cosmetics.currentPet)
			table.insert(save, player.Cosmetics.currentEffect)
			--TITLES SECTION 1
			table.insert(save, player.Bools.Beginner.Value)
			table.insert(save, player.Bools.Star.Value)
			table.insert(save, player.Bools.Rookie.Value)
			table.insert(save, player.Bools.Veteran.Value)
			table.insert(save, player.Bools.VIP.Value)
			table.insert(save, player.Bools.Skilled.Value)
			table.insert(save, player.Bools.Pro.Value)
			table.insert(save, player.Bools.Master.Value)
			table.insert(save, player.Bools.Expert.Value)
			table.insert(save, player.Bools["Game Creator"].Value)
			table.insert(save, player.Bools["Obby God"].Value)
			table.insert(save, player.Bools.dan.Value)
			--EFFECTS SECTION 1
			table.insert(save, player.Bools["Classic Fire"].Value)
			table.insert(save, player.Bools["No Effect"].Value)
			table.insert(save, player.Bools["VIP Fire"].Value)
			table.insert(save, player.Bools["Cosmic Flames"].Value)
			table.insert(save, player.Bools["Rainbow Flames"].Value)
			--PETS SECTION 1
			table.insert(save, player.Bools["No Pet"].Value)
			table.insert(save, player.Bools["Ghost"].Value)
			table.insert(save, player.Bools["Lizard"].Value)
			table.insert(save, player.Bools["Apple"].Value)
			table.insert(save, player.Bools["Banana"].Value)
			table.insert(save, player.Bools["Cousin Tentacles"].Value)
			table.insert(save, player.Bools["Dr. Ishmael"].Value)
			table.insert(save, player.Bools["Mr. Tentacles"].Value)
			table.insert(save, player.Bools["Mrs. Tentacles"].Value)
			table.insert(save, player.Bools["Rainbow Squid"].Value)
			table.insert(save, player.Bools["Staring Cat"].Value)
			table.insert(save, player.Bools["Turtle"].Value)
			table.insert(save, player.Bools["Pop Cat"].Value)
			table.insert(save, player.Bools["Panda"].Value)
			table.insert(save, player.Bools["Fish"].Value)
			table.insert(save, player.Bools["Duck"].Value)

			ds:SetAsync(player.UserId, save)
		end)()
	end
	wait(2)
end)

This is unrelated to your datastore issue but
SO MUCH REPEATING CODE
This would be a nightmare to edit.
Instead, loop through a table of values:

local Pets = {
    "Mrs.Tentacles",
    "Mr. Tentacles"
-- you get the idea
}
local stats = ds:GetAsync(player.UserId)
for i, v in pairs(Pets) do
     
     local value = instance.new("BoolValue") 
     if stats ~= nil then
         value.Value = stats[i]
     end
     --parenting after property changes is faster
     value.Parent = leaderstats
end
2 Likes

That’s what I did the first time and everything was working fine and after a day the DataStore completely stopped working.

That’s the error I receive.

Added it and it didn’t seem to work.

I think you may have to use HttpService:JSONEncode() and HttpService:JSONDecode() to convert your table into a string that can be stored and then decoded when needed

Thank you, I’ll implement that right now and come back with an update.

Good luck, if you need any help with doing this, I can try to see how I can

I can’t seem to get it to work I get an error saying Argument 1 is missing or nil.

Could you show me what you’re trying to do

local statsO = ds:GetAsync(player.UserId)
local stats = HttpService:JSONDecode(statsO);

If it’s not saving then GetAsync will return nil

it errors on that second line it says statsO is nil which is leading me to believe there is something going wrong when it saves

Okay, do you have any suggestions on how to get it to save? That was my original problem and it still persists no matter what I try

What you could do is make it Decode if stats is not nil, aka, the first line in the if stats ~= nil then loop, make a variable inside which contains the decoded table and use that for reference to set the values.

And in the PlayerRemoving event, you already have it in a table, so jsut Encode that and save it

1 Like