Why is my datastore not saving?

Hi, I wish to make a datastore that saves the value of how many times a person has died. I even added a pcall function to check but It doesn’t seem to print anything on the output. Does anyone know a solution? This is a whole script in ServerScriptService.

local DataStore = game:GetService(“DataStoreService”)
local MyDataStore = DataStore:GetDataStore(“MyDataStore”)

game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new(“Folder”, player)
leader.Name = “leaderstats”
local deaths = Instance.new(“IntValue”)
deaths.Parent = leader
deaths.Name = “Deaths”

local userID = "Player_"..player.UserId

local data
local success, errormessage = pcall(function()
	data = MyDataStore:GetAsync(userID)
end)

if success then
	deaths.Value = data
end

game.Players.PlayerRemoving:Connect(function(player)
local userID = “Player_”…player.UserId
local data = player.leaderstats.Deaths.Value
local success, errormessage = pcall(function()
MyDataStore:GetAsync(userID, data)
end)

if success then
	print("Datastore successfully stored!")
else
	print("Error detected within datastore!")
	warn(errormessage)
end

end)

player.CharacterAdded:Connect(function(char)
	local humanoid
	repeat
		humanoid = char:FindFirstChild("Humanoid")
	until humanoid
	humanoid.Died:Connect(function()
		deaths.Value = deaths.Value + 1
	end)
end)

end)

Is there any error? Try playing it in a real game and not studio.

You’re calling GetAsync instead of SetAsync in your PlayerRemoving event

1 Like

There’s no error on the output and same results in-game.

Ah thanks. I didn’t realise the fact I wrote GetAsync.