Datastore could not save my datas

Hello!
I don’t know why but my datas could not saves i think the player remove too fast and the script dont have the time for, here is the script :

local DataStoreService = game:GetService("DataStoreService")
local Saves = DataStoreService:GetDataStore("Saves")

local function getdatas(UserId)
	local playerId = "Player_" .. UserId

	local success, result = pcall(function()
		local playerData = Saves:GetAsync(playerId) or BasicSave
		return playerData
	end)

	if success then
		return result
	else
		warn("an error RAHHHH " .. tostring(UserId) .. ": " .. result)
		return BasicSave
	end
end

local function checkdatas(player, types)
	local playerData = getdatas(player)

	local coins = playerData[1]
	local avatarWearing = playerData[2]
	return playerData[3]
end

local function savedatas(player)
	local playerId = "Player_" .. player.UserId

	local success, error = pcall(function()
		local avatarowner = checkdatas(player) or {"Default"}
		local playerData = {player.leaderstats.Coins.Value, tostring(player:WaitForChild("AvatarWearing").Value), avatarowner}
		Saves:SetAsync(playerId, playerData)
	end)

	if not success then
		warn("Error saving data for player " .. player.Name .. ": " .. tostring(error))
	end
end

--------------


game:GetService("Players").PlayerAdded:Connect(function(plr)
	local d = getdatas(plr.UserId)
	local av = Instance.new("StringValue")
	av.Name = "AvatarWearing"
	av.Parent = plr
	av.Value = d[2]

	local leader = Instance.new("Folder")
	leader.Name = "leaderstats"
	leader.Parent = plr

	local coins = Instance.new("NumberValue")
	coins.Name = 'Coins'
	coins.Value = d[1]
	coins.Parent = leader
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
	savedatas(plr)
end)

Thanks,
~ @hollaquetalBRUH

3 Likes

BasicSave is empty. You surely forgot to put something there

try adding this on the top of the code

local BasicSave = {0," "," "," "}

BasicSave is here if forgot to give you the line :

local BasicSave = {0, "Default", {"Default"}}

when i try to print coins when the player leave it always return me 0 and save 0 instead of what i have

Maybe the problem is not the actual saving process.

I see an issue.
In the “getdatas” function, you use a parameter called “UserId”.
Problem is in the “checkdatas” function which uses the “getdatas” function, you don’t pass the player.UserId but just the player instance.
Now if we look at the variable playerId in the function “getdatas”, this will cause a conflict.

Let’s say we have Player1 with a UserId of 1234, the way you scripted your code local playerId would be:

local playerId = "Player_Player1"
-- and not:
local playerId = "Player_1234"

Now this is a problem because in the “savedatas” function the playerId is defined correctly.
Player1 would have following variable then:

local playerId = "Player_1234"
-- and not:
local playerId = "Player_Player1"

Fix this issue in your “checkdatas” function.

local function checkdatas(player, types)
    local playerData = getdatas(player.UserId) -- we pass the UserId with it as required in "getdatas" function
    -- code continues here
end

Another question. You use a second parameter called “types” in your “checkdatas” function, but you never use it.
Remove it when it’s unnecessary to make your code more readable and easier to edit.

If this problem persists, reply to this post.