DataStore Issues

I have tried multiple different ways to save this data, but it seems not to work. The SaveCreateData seems to work and process, but JobDataApply is not saving for some reason. When a player applies it sends it to the server to process it through httpService, but then tries to save it on the server through a BindableEvent. Instance of this code:

jobData.Event:Connect(function(arg, name, jobId, player)
	local AJF = player:FindFirstChild("AppliedJobsFolder")
	local CJF = player:FindFirstChild("CreatedJobsFolder")

	if arg == "SaveCreate" then
		local JobCreate = Instance.new("StringValue", CJF)
		JobCreate.Name = name
		JobCreate.Value = jobId
		
	elseif arg == "SaveApply" then
		local JobApply = Instance.new("StringValue", AJF)
		JobApply.Name = name
		JobApply.Value = jobId
	end
end)

Both of the creating instances work, and I can see that they have been created in studio. I also tried the switch over to serverside, and it was still present.

I then try to save the data by saving everything in the folder, but the applyData does not want to save. I have tried to make it work, but I don’t know why it won’t work. It would make sense if both of them didn’t, but only the applyData doesn’t work, even the print data on the event PlayerRemoving it prints out the data. Instance of this code:

local ds = game:GetService("DataStoreService")
local SaveDataCreate = ds:GetDataStore("JobDataCreate")
local JobDataApply = ds:GetDataStore("JobSaveApply")

game.Players.PlayerAdded:Connect(function(player)
	local playerkey = "id_" .. player.UserId

	local AJF = Instance.new("Folder", player)
	AJF.Name = "AppliedJobsFolder"

	local CJF = Instance.new("Folder", player)
	CJF.Name = "CreatedJobsFolder"

	local data1 = SaveDataCreate:GetAsync(playerkey) or {}
	local data2 = JobDataApply:GetAsync(playerkey) or {}

	print("Data1:", data1)
	print("Data2:", data2)

	for _, obj in pairs(data1) do
		local stringVal = Instance.new("StringValue", CJF)
		stringVal.Name = obj[1]
		stringVal.Value = obj[2]
	end

	for _, obj in pairs(data2) do
		local stringVal2 = Instance.new("StringValue", AJF)
		stringVal2.Name = obj[1]
		stringVal2.Value = obj[2]
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local AJF = player:FindFirstChild("AppliedJobsFolder")
	local CJF = player:FindFirstChild("CreatedJobsFolder")
	local playerkey = "id_" .. player.UserId

	local data1 = {}
	local data2 = {}

	for _, v in pairs(AJF:GetChildren()) do
		table.insert(data1, {v.Name, v.Value})
	end

	for _, x in pairs(CJF:GetChildren()) do
		table.insert(data2, {x.Name, x.Value})
	end

	print("Saving Data1:", data1)
	print("Saving Data2:", data2)

	JobDataApply:SetAsync(playerkey, data1)
	SaveDataCreate:SetAsync(playerkey, data2)
end)

If you have any idea of how to fix this please let me know!

Edit:
There is an issue with both.

I say this on most datastore posts but try adding this to the end of your save script

game:BindToClose(function()
    task.wait(3)
end)

This gives the game time to save before the server shuts down.

First off, I’m decently sure without the API enabled setting on (Configure place setting) that you can’t use datastores in studio (You can’t save to them). Also pretty sure datastores are only accessible on the server. If these things don’t fix it, we’ll continue with problem solving.

1 Like

I also know you cannot store instances in a datastore and I’m decently sure a folder is an instance. I would suggest using getchildren() on the folder and either storing them in a table or as a stringvalue, depending on what you have stored in that folder.

Nevermind, I see you were already aware of that.

I believe he is already doing that.

1 Like

Yeah, would’ve helped to read the code, right? My bad :slight_smile: I wanted to leave my reply in case somebody sees it that doesn’t know. EDIT : Please also provide the error you received if you received one, because knowledge of that can help us debug what’s going on.

1 Like

The thing is I am receiving no errors. The only error I know is that it doesn’t load when I join back. Like the PlayerRemoving Event prints out what should be saved, but when the player rejoins, it doesn’t save. And I double checked it wasn’t something as simple as not having the settings turned on in studio, but it isn’t working. Like I have done saving folders in other games with DataStores, but today and yesterday, I can’t seem to have it work. Also I tried tostring() and that did not work either.

I’m actually pretty sure the first post by poly is correct, in that the game needs time to execute those commands brought on by the playerremoving event. Here is another way to do what poly said: Data Store not saving after leaving

Wow, I wasn’t expecting that to work, because usually without that, it works just fine. Thank you for the help!

1 Like

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