Data won't save if player leaves before like 20 seconds

Hello I’m using datastoreservice and for some reason player data won’t store unless the player leaves after I’m guessing roughly 20 seconds. Somebody please help.

Can we see your script?
We can’t help if you have no script.
I’m guessing you have a task.wait() or wait() that’s what is causing it
if so, there is a solution for that.

Ok here is my script:

local DataStoreService = game:GetService("DataStoreService")

local PartsStore = DataStoreService:GetDataStore("PlayerParts")

local data = 0
game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local PartColor = Instance.new("Color3Value", leaderstats)
	PartColor.Name = "Color"
	PartColor.Value = Color3.new(255,255,255)
	local Parts = Instance.new("IntValue", leaderstats)
	Parts.Name = "Parts"
	
	local playerUserID = ("Player_" ..  player.UserId)
	
	local success, errorMessage = pcall(function()
		data = PartsStore:GetAsync(playerUserID)
	end)
	if success then
		Parts.Value = data
	else
		print("Player's data wen't no")
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = ("Player_" .. player.UserId)
	
	local data = player.leaderstats.Parts.Value
	
	local success, errorMessage = pcall(function()
		PartsStore:SetAsync(playerUserID, data)
	end)
	if success then
		print("Data Saved")
	else
		print("Data Fricked Up")
	end
end)

The script looks fine, Add this function in the script and then test it in-game.

function OnShutDown()
	task.wait(4)
end

game:BindToClose(OnShutDown)

Didn’t seem to work. Where should I put it? Currently I have it at the start of the player.removing

Use ProfileService instead, it will benefit you way more than if you set it up yourself.

I’m not trying to copy and my script does the same thing’s but less functions ahead of time.

maybe try doing print on the error so you can see what it is doing
your script seems to be ok from what I read
and not sure if this is in studio just make sure it has api access

	else
	print("Data Fricked Up " .. errorMessage)

may even unwrap that pcall and get the error from the setasync in studio

Are you testing in studio?
Test it in-game please.

It already has API service allowed. So I don’t really know why the data won’t save.

it might be the value not being there anymore or issue with it that is one reason i keep a table in my save script of all the players current data even if they player object or any object under it is gone the data is still in the script and can be saved by the key for that player

try a print on the data before the set async prints are your friend on debugging that and errors

Well that couldn’t be the problem in my case. I have multiple other scripts that would send an error if it wasn’t there.

whats the error that the setasync pcall is throwing?

Nothing. The data saving won’t run I don’t know why.

Here is my output:

16:46:38.640  Requiring asset 512742721.
Callstack:
cloud_8428896693.archimedesTwo.initiatePlugin.MainModule, line 29
cloud_8428896693.archimedesTwo.initiatePlugin.MainModule, line 28
  -  Server
  16:46:38.706  loadstring() is not available  -  Server
  16:46:38.706  Stack Begin  -  Studio
  16:46:38.706  Script 'cloud_183586902.Build v4.1.Plugin', Line 30 - function LoadResource  -  Studio
  16:46:38.706  Script 'cloud_183586902.Build v4.1.Plugin', Line 32  -  Studio
  16:46:38.706  Stack End  -  Studio
  16:46:38.821  Requiring asset 518094091.
Callstack:
cloud_8428896693.archimedesTwo.initiatePlugin.MainModule, line 30
cloud_8428896693.archimedesTwo.initiatePlugin.MainModule, line 28
  -  Server
  16:46:39.669  loadstring() is not available  -  Client
  16:46:39.669  Stack Begin  -  Studio
  16:46:39.669  Script 'cloud_183586902.Build v4.1.Plugin', Line 30 - function LoadResource  -  Studio
  16:46:39.669  Script 'cloud_183586902.Build v4.1.Plugin', Line 32  -  Studio
  16:46:39.669  Stack End  -  Studio

i don’t see anything from the save script…
not even the prints for sucess or error

Yeah. It won’t run other than the leaderstats loading.

Can you try this??

local DataStoreService = game:GetService("DataStoreService")

local PartsStore = DataStoreService:GetDataStore("PlayerParts")

local data = 0
game:GetService("Players").PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local PartColor = Instance.new("Color3Value", leaderstats)
	PartColor.Name = "Color"
	PartColor.Value = Color3.new(255,255,255)
	local Parts = Instance.new("IntValue", leaderstats)
	Parts.Name = "Parts"
	
	local playerUserID = ("Player_" ..  player.UserId)
	
	local success, errorMessage = pcall(function()
		data = PartsStore:GetAsync(playerUserID)
	end)
	if success then
		Parts.Value = data
	else
		print("Player's data wen't no")
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = ("Player_" .. player.UserId)
	
	local data = player.leaderstats.Parts.Value
	
	local success, errorMessage = pcall(function()
		PartsStore:SetAsync(playerUserID, data)
	end)
	if success then
		print("Data Saved")
	else
		print("Data Fricked Up")
	end
end)

function onShutDown()
task.wait(4)
end

game:BindToClose(onShutDown)
1 Like

your script works
https://gyazo.com/1df48eca42e6c5b37fb831c4d8c013c0

where do you have it located?

ServerScriptService. uh get this message to 30