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

Did 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)

That work’s but I want to stick to my script instead of copying, sorry.

This is the same as yours but it has the closing function.

We are using something called BindToClose()
This when the server shut downs it waits 4 seconds to save every player data.

Here is an example

function onShutDown()
-- Server is shutting down, There is no time to save players data.
task.wait(4) -- It will wait 4 seconds then closes the server. Now we have time to save the data!
end

game:BindToClose:Connect(OnShutDown)

Oh. I see. Thank you then.


you have something else going on there then.
you said it is loading the player leaderstats?

setup a warn print at the bottom of your addplayer and the bottom of your player removing and also one at the bottom of the script see if any show in output

I don’t really know what the problem is, it just won’t load.

I also have an issue that the playerdata will not load. But I clearly have a “Parts” = “Data”

something like warn(‘PlayerAdded Loaded…’)

Didn’t the script I sent you work??

is this your full output ? seems there should be error from that script if its enabled

Yes, that’s the full output. I included plugin outputs too so.

did you try doing the warn prints in the script to see if the script is even running at all