Datastore troubles

Heya, I fixed the SetPlayerData function. Now I am stuck on the exitdata function. The fix for the SetPlayerData was just a typo, but I see nothing wrong with my exit function.

local PlayerData = {}

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


local data = {
	
	['Points'] = 0,
	['EXP'] = 0,
	['MaxEXP'] = 100,
	['Defense'] = 0,
	['Strength'] = 0,
	['Agility'] = 0,
	['Speed'] = 0,
	['MaxHealth'] = 150
}

local strings = {
	
	['testdatastring'] = "ABC"
}


local function SetPlayerData(Player)
	local plrdata = nil
	local playerUserId = 'Player_'..Player.UserId
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = Player
	
	
	for stat, value in pairs(data) do
		local NumStats = Instance.new('IntValue')
		NumStats.Name = stat
		NumStats.Value = value
		NumStats.Parent = leaderstats
	end
	
	local success, err = pcall(function()
		plrdata = DataStore:GetAsync(playerUserId)
	end)
	if success then
		print('yoyo')
	elseif err then
		warn('yikes!')
		end
		if plrdata then
			for stat, value in pairs(data) do
			Player.leaderstats[stat].Value = value
			end
			print('hey')
		else
			warn('oh noes')
		end
    end

    local function exitdata(Player)
    	local datatable2 = {}
    	local playerUserId = "Player_"..Player.UserId
    	for stat, value in pairs(Player.leaderstats:GetChildren()) do
    		datatable2[value] = value.Value
    	end
    	local success = pcall(function()
    		DataStore:UpdateAsync(playerUserId)
    	end)
    	if success then
    		print("YESSIR")
    	else
    		warn(":(")
    	end
    end

    game.Players.PlayerAdded:Connect(SetPlayerData)

    game.Players.PlayerRemoving:Connect(exitdata)


    return PlayerData

Help would be appreciated I know I’m missing something obvious.

1 Like

Well, in your SetPlayerData you’re not saving any values to the datastore, but trying to retrieve possibly existing data (might just be confusing function namecalling).

And in your exitdata, the second argument (which is supposed to be a function) to the :UpdateAsync method is empty… meaning the datastore has no idea on how to update the supposedly existing datastore.

Alright. So in your SetPlayerData function, you get to know that a save does not currently exist for the player, meaning that you need to set a save (usually a default value/array which you have predefined) for the said player using the :SetAsync method.

1 Like

Sorry for late reply, was busy.

In the SetPlayerData, how would I save the values? I’m saving them in a table so I might be saving it the wrong way. I don’t like to use SetAsync much cause it has lead to some, data loss in the past. But if necessary, I’ll use it.

As for the exit what do you mean by it’s empty? The parameters are there.

1 Like

Well, I fixed the data setting up. :smiley:

I was using the wrong parameter to save the data. Whoops.

But now I’m stuck on the player removing… Help is much appreciated.

1 Like

DataStore:UpdateAsync(playerUserId)okay so, is cool you update the data but, with what value/string ??
This is more good i tink DataStore:UpdateAsync(playerUserId,"Hey im saved in the player data")
If is not work try this

local function saveValues(player)
	local saveData = {}
	local StatsFile = player.leaderstats

	for i,v in pairs(StatsFile:GetChildren()) do
			saveData[#saveData + 1] = v.Name
			saveData[#saveData + 1] = v.Value
	end
	ds:SetAsync(player.UserId, saveData) -- I know is not the best use you can lost data
end

This work good normaly

After, saving in roblox studio don’t work and can cose error so use this for verify is you are in studio or not before try to save so you can use IsStudio

	if not game:GetService("RunService"):IsStudio() then

	end

After all that, you need to save when the server have a shutdown request for prevent data lost so you can use BindToClose

game:BindToClose(function()

end)

No. This is definitely not what the method expects as its second argument. The second argument to :UpdateAsync, as I already said, is supposed to be a function.

1 Like

i think

Okay srr i not have view you comments and im not using UpdateAsync so i can’t know

Well, when I did try and use a function for updateasync, I got no errors in the output. On the contrary, I got nothing to print the successful save of the datastore. I have an updated one, I’ve tried using different methods to updateasync but they don’t work. Even tried wrapping the async inside a pcall, but, ya know… It didn’t work. That just gave me a warning.

local PlayerData = {}

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


local data = {
	
	['Points'] = 0,
	['EXP'] = 0,
	['MaxEXP'] = 100,
	['Defense'] = 0,
	['Strength'] = 0,
	['Agility'] = 0,
	['Speed'] = 0,
	['MaxHealth'] = 150
}

local strings = {
	
	['testdatastring'] = "ABC"
}


local function SetPlayerData(Player)
	local plrdata = nil
	local playerUserId = 'Player_'..Player.UserId
	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'leaderstats'
	leaderstats.Parent = Player
	
	
	for stat, value in pairs(data) do
		local NumStats = Instance.new('IntValue')
		NumStats.Name = stat
		NumStats.Value = value
		NumStats.Parent = leaderstats
	end
	
	local success, err = pcall(function()
		plrdata = DataStore:GetAsync(playerUserId, data)
	end)
	if success then
		print('yoyo')
	elseif err then
		warn('yikes!')
		end
		if plrdata then
			for stat, value in pairs(data) do
			Player.leaderstats[stat].Value = value
		end
			print('hey')
		else
			warn('oh noes')
		end
end

local function exitdata(Player)
	local datatable2 = {}
	local playerUserId = "Player_"..Player.UserId
	for stat, value in pairs(Player.leaderstats:GetChildren()) do
		datatable2[value.Value] = value.Value
	end
	local success = pcall(function()
		DataStore:UpdateAsync(playerUserId, function(datatable2)
			local newval = datatable2 or 0
			newval = newval
		end)
	end)
	if success then
		print("YESSIR")
	else
		warn(":(")
	end
end

game.Players.PlayerAdded:Connect(SetPlayerData)

game.Players.PlayerRemoving:Connect(exitdata)


return PlayerData