Datastore is not working, even with pcall

my datastore is not working, I added a print but for some reason even with a PCALL it doesnt work, like how does that make sense, heres the code:

	local cs = player.CharacterStats
	local items = {cs.ShirtVal.Value, cs.PantsVal.Value, cs.FaceVal.Value, cs.HairVal.Value, cs.Customized.Value,
		cs.Gender.Value, cs.FirstName.Value, cs.LastName.Value, cs.ColorVal.Value, cs.Height.Value, cs.Weight.Value, cs.Minutes.Value, cs.Hours.Value
	}
	local key = "id-"..player.userId
	print("savescript savin: "..key)
	print(items)
	local success,err
	success,err = pcall(function()
		datastore:SetAsync(key, items)
	end)
	if not success then
		print(err)
	end
	print("savescript  Finished")

at the end its supposed to print savescript finished, but it wasnt doing that, then I put a pcall for the datastore, still not working. when i print the variable items it works, but it doesnt work after. heres an ‘explanation’:

	print("savescript savin: "..key)
	print(items) --last piece of code that works
	local success,err
	success,err = pcall(function() --pcall, to make sure even if it doesnt work its gonna run
		datastore:SetAsync(key, items)
	end)
	if not success then
		print(err)
	end
	print("savescript  Finished") -- does not run

I think what’s happening is that Studio is closing the playtest server too quickly for SetAsync to finish working, so this should work to fix that:

	local cs = player.CharacterStats
	local items = {cs.ShirtVal.Value, cs.PantsVal.Value, cs.FaceVal.Value, cs.HairVal.Value, cs.Customized.Value,
		cs.Gender.Value, cs.FirstName.Value, cs.LastName.Value, cs.ColorVal.Value, cs.Height.Value, cs.Weight.Value, cs.Minutes.Value, cs.Hours.Value
	}
	local key = "id-"..player.userId
	print("savescript savin: "..key)
	print(items)

	local success, error = pcall(datastore.SetAsync, datastore, key, items)

	if not success then
		warn(error)
	end

	print("savescript finished")

	if game:GetService("RunService"):IsStudio() then
		game:BindToClose(function()
			task.wait(1)
		end)
	end

that did not solve my problem, but it was related to the issue, ingame it works fine, in studio it doesnt work at all. thank you for your help

2 Likes

I’m glad that you were able to solve the problem, but it would be nice if possible to explain how you did in-order to aid others that experience a similar issue :slight_smile::+1:

Creating the datasave in studio sometimes gives problems, so instead publish then go ingame. After creating the datasave ingame leave and now you can load it in studio, for some reason my only problem was ‘creating’ the datasave in studio, now it saves and loads just fine.

1 Like

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