My datastore is not working and I have no idea what's wrong

Hi there, so I have been using this Datastore for a while and it just randomly stopped working I have no idea why because it has been working for the past 1-2 months but now it just stopped.

If you have any ideas on how I can fix it or why it broke please let me know as fast as you can this is for my game that I need to update today.

I did hide my key’s and datastore name these aren’t the real ones

Thank you

local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDatastore")
local Market = game:GetService("MarketplaceService")

--

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		if not player:FindFirstChild("leaderstats") then
			--- leaderstats
			
			local leaderstats = Instance.new("Folder",player)
			local Rings = Instance.new("NumberValue",leaderstats)
			local Yuans = Instance.new("NumberValue",leaderstats)
			local CompletedQuest = Instance.new("BoolValue",player)
			CompletedQuest.Name = "CompletedQuest"
			Rings.Name = "Rings"
			Yuans.Name = "Yuans"
			leaderstats.Name = "leaderstats"
			
			--- Datastore
			
			local data
			local YuansData
			local GliderData
			local QuestData
			local folder = character:WaitForChild("OwnedGliders")
			--local folder = player.Character:WaitForChild("OwnedGliders")
			
			local success,errormessage = pcall(function()
				data = DataStore:GetAsync(player.UserId.."MyKey1")
				YuansData = DataStore:GetAsync(player.UserId.."MyKey2")
				QuestData = DataStore:GetAsync(player.UserId.."MyKey3")
				GliderData = DataStore:GetAsync(player.UserId.."Mykey4")
			end)
			
			if success then
				Rings.Value = data
				Yuans.Value = YuansData
				CompletedQuest.Value = QuestData
				
				for i,Glider in pairs(GliderData) do
					local NewValue = Instance.new("StringValue",folder)
					NewValue.Name = Glider
					NewValue.Value = Glider
				end
			else
				warn("DataError: "..errormessage.." - "..player.Name)
			end

			while true do
				wait(math.random(120,200))
				
				if player.MembershipType == Enum.MembershipType.Premium then
					player.leaderstats.Yuans.Value += math.random(5,10)
				end
				
				if Market:UserOwnsGamePassAsync(player.UserId,11855328) then
					player.leaderstats.Yuans.Value += math.random(7,15) * 2
				else
					player.leaderstats.Yuans.Value += math.random(10,20)
				end
			end
			
		end
	end)
end)


----

game.Players.PlayerRemoving:Connect(function(player)
	local folder = player.Character:FindFirstChild("OwnedGliders")
	local GliderData = {}
	
	for i,Glider in pairs(folder:GetChildren()) do
		GliderData[i] = Glider.Value
	end
	
	local success,errormessage = pcall(function()
		DataStore:SetAsync(player.UserId.."MyKey1",player.leaderstats.Rings.Value)
		DataStore:SetAsync(player.UserId.."MyKey2",player.leaderstats.Yuans.Value)
		DataStore:SetAsync(player.UserId.."MyKey3",player.CompletedQuest.Value)
		DataStore:SetAsync(player.UserId.."MyKey4",GliderData)
	end)
	
	if success then
		print("saved data".." - "..player.Name)
	else
		warn("failed to save data: "..errormessage.." - "..player.Name)
	end
end)
3 Likes

If you are testing this in the studio, test it in-game instead. Other than that, there doesn’t seem to be any problem.

I’ve tested it in Studio and in game but nothing seems to work

1 Like

are there any errors when getting the data?

no there are not. although sometimes there are errors looping threw the “OwnedGliders” folder/table but it still saves the data even when it errors

1 Like

I would recommend adding some checks on whether the data is there or not like this:

if success then
if data then
Rings.Value = data
end
if YuansData then
Yuans.Value = YuansData
end
if QuestData then
CompletedQuest.Value = QuestData
end
if GliderData then
for i,Glider in pairs(GliderData) do
local NewValue = Instance.new("StringValue",folder)
NewValue.Name = Glider
NewValue.Value = Glider
end
end
else
warn("DataError: "..errormessage.." - "..player.Name)
end

Sorry it’s a little messy, I’m not on studio atm

its alright ill test out to see if adding if statements helps out

1 Like

ok I tested out if statements and it doesn’t seem to change anything so

1 Like

I assume this is because it already overwrote your old data. You will need to change all your data again and test it.

ok I tried that out I reset my data using the Datastore editor plugin and well the only thing is changed is now my data is 0 but even if it did work what about all the others people that play the game? I would need to reset there data.?

1 Like

I’m pretty sure that’s a bit excessive and you really don’t need to hide your key, reason being is that you can only access your datastore through your game. It is not like a key being used for an external datastore

try doing

data = DataStore:GetAsync(player.UserId.."MyKey1") or 0 

add the or 0 to all of them

If that doesn’t work, then potentially you may be getting an infinite yield somewhere, maybe herelocal folder = character:WaitForChild("OwnedGliders")

Not sure if you can store a indexable array in a datastore, so try json encode for GliderData

Other than that, I would player.CharacterAdded and its respective end, because that’s excessive.

If you are testing in Studio, make sure you have Enable Studio Access to API Services. In game settings.

yes ik I didn’t need to hide the keys but I did cuz why not lol and doing “or 0” will mainly help if the value is nil so I added if statements to check if there is a value there before actually changing the value and if the WaitForChild was causing an infinite yield it would print that in the output right?

1 Like

I would use table.insert() here.

I mean what’s odd is that you aren’t getting an output

yeah

yeah

Also, make sure you enabled Http Service.

it is already on so it must not be doing anything

1 Like

Are you sure there are no errors in the console? Or possibly another script preventing the datastore from functioning correctly?

yes there are no errors and there shouldn’t be any other scripts that interfere with it

Thank you to everyone who tried to help I got my friend to help me recode it and it is working now