What is wrong with my datastore script?

Why is it not saving the equippedskin value?

local datastore = game:GetService("DataStoreService"):GetDataStore("AfkData")


game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder", player)
	folder.Name = "leaderstats"

	local AFK = Instance.new("IntValue", folder)
	AFK.Name = "Minutes"
	
	local EquippedVal = Instance.new("StringValue", folder)
	EquippedVal.Name = "EquippedSkin"
	local plrkey = "id_"..player.UserId
	
	local GetSaved = datastore:GetAsync(plrkey)
	if GetSaved then
		AFK.Value = GetSaved[1]
		EquippedVal.Value = GetSaved[2]
	else
		local numbersforsaving = 
			{AFK.Value, EquippedVal.Value}
		datastore:GetAsync(plrkey, numbersforsaving)
	end
	
	for i, v in pairs(workspace.Items:GetChildren()) do
		if EquippedVal.Value == v.Name then
		local CharacterClone = v:Clone()
		print("Cloned")
		CharacterClone.Name = player.Name
		print(CharacterClone.Name)
		local cframe= player.Character.HumanoidRootPart.CFrame
		player.Character:Destroy()
		print("Destroyed")
		player.Character = CharacterClone
		print("Nearly")
		local root = CharacterClone:WaitForChild("HumanoidRootPart")
		local hum = CharacterClone:WaitForChild("Humanoid")
		wait()
		root.CFrame = cframe
			CharacterClone.Parent = game.Workspace
		end
	end
end)

local RunService = game:GetService("RunService")

if not RunService:IsStudio() then -- if we're playtesting in studio, we'll just skip this part of the code
	game:BindToClose(function()
		if #game.Players:GetPlayers() <= 1 then return end -- returns if the player count is equal to or less than 1

		for _, player in pairs(game.Players:GetPlayers()) do
			datastore:SetAsync("AfkData_"..player.UserId)
		end
	end)
end
game.Players.PlayerRemoving:Connect(function(player)
	datastore:SetAsync("id_"..player.UserId, {player.leaderstats.EquippedSkin.Value, player.leaderstats.Minutes.Value})
end)

This should be SetAsync, right?

no? What do you mean?

A couple things:

  • You aren’t encasing your DataStore functions in pcalls, which would more than likely be the issue as to why your data is not saving/loading

  • There’s no need the call the GetAsync function twice, only call it once

  • Add print statements in your DataStore functions to know what succeeded & what didn’t

1 Like

So why does GetAsync have another argument?
key should be the only parameter.

1 Like

Also, if your play testing in studio, the data save probably won’t work, try in a real game. Wrap in pcalls, and it is a good idea to maybe save every player’s data in the bind to close, which is why it might not be workin