DataStore Not Working?

So my dilemma is that I have this simple data script that is used for saving player data. I have no idea what is wrong with this. It’s not saving anything.

Code:

local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local playerData = DSS:GetDataStore("testData1")

local baseData = {
	wins = 0;
	coins = 0;
	kills = 0;
	claimed = 0;
	reset = false
}

function checkTableEquality(t1,t2)
	for i,v in next, t1 do if t2[i]~=v then return false end end
	for i,v in next, t2 do if t1[i]~=v then return false end end
	return true
end

Players.PlayerAdded:Connect(function(player)
	-- creating stats
	local leaderstats = Instance.new("Folder")
	leaderstats.Archivable = false
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local generalstats = Instance.new("Folder")
	generalstats.Archivable = false
	generalstats.Name = "generalstats"
	generalstats.Parent = player
	
	-- creating values
	local Vwins = Instance.new("IntValue")
	Vwins.Name = "Wins"
	Vwins.Parent = leaderstats
	
	local Vcoins = Instance.new("IntValue")
	Vcoins.Name = "Coins"
	Vcoins.Parent = generalstats 
	
	local Vkills = Instance.new("IntValue")
	Vkills.Name = "Kills"
	Vkills.Parent = generalstats 

	local Vclaimed = Instance.new("IntValue")
	Vclaimed.Name = "Claimed"
	Vclaimed.Parent = generalstats 

	local Vreset = Instance.new("BoolValue")
	Vreset.Name = "Reset"
	Vreset.Parent = generalstats
	
	local Vwins2 = Vwins:Clone()
	Vwins2.Parent = generalstats
	
	-- getting player data
	local currentData
	local fail, result = pcall(function()
		currentData = playerData:GetAsync("dataKey-"..tostring(player.UserId))
	end)
	
	-- checking if they have any saved data
	if result then
		Vwins.Value = currentData.wins
		Vwins2.Value = currentData.wins
		
		Vcoins.Value = currentData.coins
		Vkills.Value = currentData.kills
		Vclaimed.Value = currentData.claimed
		Vreset.Value = currentData.reset
	else
		playerData:SetAsync("dataKey-"..tostring(player.UserId), baseData)
		
		Vwins.Value = 0
		Vwins2.Value = 0
		
		Vcoins.Value = 0
		Vkills.Value = 0
		Vclaimed.Value = 0
		Vreset.Value = false
	end
	
	--[[game:BindToClose(function()
		local newData = {
			wins = Vwins.Value;
			coins = Vcoins.Value;
			kills = Vkills.Value;
			claimed = Vclaimed.Value;
			reset = Vreset.Value
		}
		
		if checkTableEquality(playerData:GetAsync("dataKey-"..tostring(player.UserId)), newData) then return end
		
		playerData:SetAsync("dataKey-"..tostring(player.UserId), newData)
	end)]]
end)

Players.PlayerRemoving:Connect(function(player)
	local Vwins = player.leaderstats.Wins
	local Vcoins = player.generalstats.Coins
	local Vkills = player.generalstats.Kills
	local Vclaimed = player.generalstats.Claimed
	local Vreset = player.generalstats.Reset
	
	local newData = {
		wins = Vwins.Value;
		coins = Vcoins.Value;
		kills = Vkills.Value;
		claimed = Vclaimed.Value;
		reset = Vreset.Value
	}
	
	playerData:SetAsync("dataKey-"..tostring(player.UserId), newData)
end)

_G.getPlayerData = function(player)
	return playerData:GetAsync("dataKey-"..tostring(player.UserId))
end

_G.setPlayerData = function(player, newData)
	local fail, result = pcall(function()
		playerData:SetAsync("dataKey-"..tostring(player.UserId), newData)
	end)
	
	if result then
		print(player.Name.."'s data was changed.")
	else
		print(player.Name.."'s data was not changed.")
	end
end
1 Like

What exactly is the issue with this? It’s not loading data, not saving data, leaderstats don’t work?

1 Like

It’s not saving anything for whatever reason.

1 Like

Ah i see, I’m pretty sure it has to do with your pcall() function. Don’t quote me on this, but I believe the returns are in the order success:Bool, error:string. Try flipping your pcall() variables and see if it changes anything,

2 Likes

It didn’t do anything, I think it’s on the right anyway idk.

1 Like

I have made a tutorial you can look at to compare your work to mine.

I hope this helps

1 Like

Try printing fail and see if it’s an HTTP error, and also have you published and tried ingame? Sometimes data stores I make don’t work in studio but work fine ingame, which might point to the issue

1 Like

Oh my god it worked, I’m so dumb.

1 Like

Called it. Ye for me i usually do success, error = pcall(), so i got confused when i saw error, success = pcall()

1 Like