Datastore and pcall not working

Hello, I am making a datastore to store the value of how many times a person has died. It doesn’t save nor print the pcall function. Does anyone know the solution?

Script is underneath.

local Datastore = game:GetService("DataStoreService")
local MDS = Datastore:GetDataStore("MDS")

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

local Death = Instance.new("IntValue")
Death.Parent = leaderstats
Death.Name = "Death"
Death.Value = 0

player.CharacterAdded:Connect(function(char)
	local humanoid
	repeat
		humanoid = char:FindFirstChild("Humanoid")
	until humanoid
	humanoid.Died:Connect(function()
		Death.Value = Death.Value + 1
	end)
	local PlayerUserid = "Player_"..player.UserId
	
	local data
	local sucess, errormessage = pcall(function()
	data = MDS:GetAsync(PlayerUserid)
	end)
	if sucess then
		Death.Value = data
	end
  end)
end)

game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserid = "Player_"..player.UserId
local data = player.leaderstats.Death.Value
local sucess, errormessage = pcall(function()
	MDS:SetAsync(PlayerUserid, data)	
end)
if sucess then
	print("Data has been successfully saved upon client's leave.")
else
	print("There was an error")
	warn(errormessage)
end
 end)

Hello!

There are two reasons that affect the saving process, but not certain that will solve the issue exactly. So I would recommend trying them in order.

First of all, I think the solution must be this one, every time you leave the game in the studio, the PlayerRemoving event sometimes won’t work properly. So BindToClose function will be better for this.

BindToClose function is processed when the server shuts down.

game:BindToClose(function()
    for i, player in pairs(game:GetService("Players":GetPlayers()) do
        local PlayerUserid = "Player_"..player.UserId
        local data = player.leaderstats.Death.Value

        local sucess, errormessage = pcall(function()
            MDS:SetAsync(PlayerUserid, data)	
        end)

        if sucess then
            print("Data has been successfully saved upon client's leave.")
        else
            print("There was an error")
            warn(errormessage)
        end
    end
end)

So secondly, the CharacterAdded event function is so complicated. You don’t need to use the Died event for the Humanoid. CharacterRemoving event would be better for the most simplified usage.

player.CharacterRemoving:Connect(function()
    Death.Value += 1
end)

Also, take out the pcall because it will call the datastore every time the character gets removed.

local PlayerUserid = "Player_"..player.UserId

local data
local sucess, errormessage = pcall(function()
    data = MDS:GetAsync(PlayerUserid)
end)

if sucess then
	Death.Value = data
else
    warn(errormessage) -- To catch the errors while being processed.
end
2 Likes

Why would a BindToClose function be useful for a datastore? I want the player’s data to save when they leave not when the server shutsdown. Can you explain in more depth of how the BindToClose function would work in my scenario?

Your script works fine for me, so I don’t really understand why you should encounter any issues as there aren’t any.

Also, you should implement two things, which is:

  1. Save data regularly (ex, every 2-3 minutes) for every player.
  2. :BindToClose() - this function binds a function to be called prior to the game shutting down.
    More information: DataModel | Roblox Creator Documentation