Pcall function on module doesn't seem to run fast enough when a player leaves

Pcall function for data saving on module doesn’t seem to run. The module is required and I can’t think of anything except that the player leaves early before the pcall even ran.

Script:

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local DataStoreService = game:GetService("DataStoreService")

local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")

local SavingMethods = require(script.SavingMethods)

-- Private Functions

local defaultData = {
	
}

local function CreateData(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats
	
	leaderstats.Parent = player	
end	

local function PlayerAdded(player)
	CreateData(player)
	
	local data
	local key = string.format(script.Parent.Name .. "%s %s", "/", player.UserId)
	
	local playerData = {
	  Points = player.leaderstats.Points.Value,
	}
	
	local success, errormessage = pcall(function()
		data = PlayerDataStore:GetAsync(key) 
	end)
	
	if success then
		
		print(string.format(player.Name .. "%s", "'s data successfully loaded"))
	else
		print(string.format("Cannot load %s %s %s %s", player.Name, "'s data", "Error code:", tostring(errormessage)))
	end
end

local function PlayerRemove(player)
	return SavingMethods.Set(player, PlayerDataStore)
end

for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemove)

Module:

local module = {}

function module.Set(player, dataStore)
	local key = string.format(script.Parent.Name .. "%s %s", "/", player.UserId)
	
	local data = {
		
		Points = player.leaderstats.Points.Value,																					
	}
	
	local success, errormessage = pcall(function()
                print("a") -- doesnt print
		dataStore:SetAsync(key, data)
	end)
	
	if success then
		print(string.format("Data successfully saved for %s", player.Name))
	else
		print(string.format("Data did not save for %s %s %s", player.Name, "Error code: ", tostring(errormessage)))
	end
	
end

function module.Update(player, dataStore)
	local key = string.format(script.Parent.Name .. "%s %s", "/", player.UserId)
		
	local data = {
		
		Points = player.leaderstats.Points.Value,																					
	}

	dataStore:UpdateAsync(key, function(oldValue)	
		local newValue = oldValue or 0	
		newValue = data.Points	
		return newValue
	end)
end

return module

get rid of return in playerremove

1 Like

Tried it, doesn’t work and that isn’t the problem.

1 Like

try making the point value an argument of module.Set in playerremove kinda like

local z = player:FindFirstChild("leaderstats"):FindFirstChild("Points").Value
SavingMethods.Set(player, PlayerDataStore, z)

The problem is that the player leaves fast before the pcall runs, any solutions?

This shouldn’t be the case, If anything the SetAsync won’t be completed in studio.

This portion should run perfectly fine. Also try saving using game:BindToClose

Using game:BindToClose to no avail. This seems strange.