Strange Data Save issue [scripts]

Hello, I am currently experiencing a problem with my script for the datasave. In fact the script works for some people, sometimes it bugs and resets for others people. Does anyone see a problem with my script? Thank you

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Creatures3")

game.Players.PlayerAdded:Connect(function(plr)

	local folder = Instance.new("Folder")
	folder.Name = "Creatures"
	folder.Parent = plr

	local data

	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync("CreatureData-"..plr.UserId)
	end)

	if success and data then
		for i,cName in pairs(data) do
			if workspace.Creatures:FindFirstChild(cName) then 
				local newval = Instance.new("BoolValue")
				newval.Name = cName
				newval.Parent = folder
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local cTable = {}

	for i, creature in pairs(plr:WaitForChild("Creatures"):GetChildren()) do
		table.insert(cTable,creature.Name)
	end

	local success, errorMsg = pcall(function()
		DataStore:SetAsync("CreatureData-"..plr.UserId,cTable)
	end)
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetChilden()) do
		local cTable = {}

		for i, creature in pairs(plr:WaitForChild("Creatures"):GetChildren()) do
			table.insert(cTable,creature.Name)
		end

		local success, errorMsg = pcall(function()
			DataStore:SetAsync("CreatureData-"..plr.UserId,cTable)
		end)
	end
end)

Maybe try print errorMsg. Probablym when pcall not successfull, it will throw error description, which can help you.

1 Like

if i do that

local success, errorMsg = pcall(function()
		data = DataStore:GetAsync("CreatureData-"..plr.UserId)
		print("hii")
	end)

then it print hi for the first errorMsg line 11 but nothing is printed in the error msg line 35. Is there is a sign somtething went wrong ?

That’s means that your data don’t saved due to smth. try print on line 46 - 48:

local success, errorMsg = pcall(function()
	DataStore:SetAsync("CreatureData-"..plr.UserId,cTable)
	print("Hi")
end)
print(errorMsg)

No the errorMsg is not printed and the Hi doesn’t too

I have already used the “CreatureData” on old backups. Maybe I should change it to Creature2Data? or does it have nothing to do with that?

	local success, errorMsg = pcall(function()
		data = DataStore:GetAsync("CreatureData-"..plr.UserId) -- I used this data on old save
	end)

Edit: no actually I’m sure that’s not the source of the problem

Does anyone know where the source of the problem might be? Please, thank you

if no one of your saves not printed anything - probably your function don’t connected (maybe your studio EnableApi not toggled on?)

I have this error message when i leave game. EnableApi is toggled on
errlr

That’s means that you typed GetChildren instead of GetChildren(), but better use GetPlayers()

1 Like

Thank you it working well lalallala

1 Like