Data stores not saving my data

This is my first time using data stores. It all works but it just won’t save my data. It even prints “data saved” but every time I re-join it just loads the data as nil (I tested this by printing the loaded data). I’ve also double and triple checked that Studio has access to API services so don’t bother asking. Here is my code:

(For reference, the script is called PlayerAdded and is inside ServerScriptService. ‘DataFolder’ is a folder inside the script just called ‘Data’ which has a few attributes made in it).

-- Variables
Players = game:GetService("Players")
SavedData = game:GetService("DataStoreService"):GetDataStore("PlayerData")
--Entities = workspace:WaitForChild("Entities")
--VFX = game:GetService("ReplicatedStorage"):WaitForChild("VFX")

-- Core
Players.PlayerAdded:Connect(function(Player)
	local ID = Player.UserId
	local DataFolder = script:WaitForChild("Data"):Clone()
	DataFolder.Parent = Player
	
	local Success, Error = coroutine.resume(coroutine.create(function()
		SavedData:GetAsync(Player.UserId)
	end))
	
	if Success then
		print(Player.Name.. "'s data loaded successfully")
		if Error ~= nil then -- Error is actually player data, we are checking if it's empty or not.
			DataFolder:SetAttribute("Accessory1", Error.Accessories[1])
			DataFolder:SetAttribute("Accessory2", Error.Accessories[2])
			DataFolder:SetAttribute("Accessory3", Error.Accessories[3])
			DataFolder:SetAttribute("Outfit", Error.Gear[1])
			DataFolder:SetAttribute("Armour", Error.Gear[2])
			DataFolder:SetAttribute("Helmet", Error.Gear[3])
			DataFolder:SetAttribute("Level", Error.Level)
			DataFolder:SetAttribute("Experience", Error.Experience)
			DataFolder:SetAttribute("SkinColour", Error.SkinColour)
		else
			print(Player.Name, "has no saved data, creating new data...")
		end
		
	else
		warn("Error loading", Player.Name.. "'s data;", Error)
	end
	
	DataFolder:SetAttribute("Loaded", true)
end)

function SavePlayerData(Player)
	local DF = Player:WaitForChild("Data")
	local ID = Player.UserId
	
	local Success, Error = coroutine.resume(coroutine.create(function()
		SavedData:SetAsync(ID, {
			["Accessories"] = {DF:GetAttribute("Accessory1"), DF:GetAttribute("Accessory2"), DF:GetAttribute("Accessory3")},
			["Gear"] = {DF:GetAttribute("Outfit"), DF:GetAttribute("Armour"), DF:GetAttribute("Helmet")},
			["Level"] = DF:GetAttribute("Level"),
			["Experience"] = DF:GetAttribute("Experience"),
			["SkinColour"] = DF:GetAttribute("SkinColour")
		})
	end))
	
	if Success then
		print(Player.Name.. "'s data saved successfully")
	else
		warn(Player.Name.. "'s data could not be saved;", Error)
	end
end

game:BindToClose(function()
	print("Server shutting down, saving player data...")
	for Index, Descendant in ipairs(Players:GetPlayers()) do
		coroutine.resume(coroutine.create(function()
			SavePlayerData(Descendant)
		end))
	end
end)

Players.PlayerRemoving:Connect(SavePlayerData)
1 Like