I need a way to stall the player leaving before it saves. Please help! Full Script:

local datastore = game:GetService("DataStoreService")
local playerdata = datastore:GetDataStore("PlayerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Time = Instance.new("IntValue", leaderstats)
	Time.Name = "Time"
	Time.Value = 0
	
	local playerid = "User_".. player.UserId
	local timedata = playerdata:GetAsync(playerid)
	print(timedata)
	
	if timedata then
		player.Character.HumanoidRootPart.TimeTag.TextLabel.Text = timedata
		print("found value")
	else
		print("new person")
	end
end)

-- PROBLEM HERE
game.Players.PlayerRemoving:Connect(function(player)
	local success, fail = pcall(function()
		local playerid = "User_".. player.UserId
		-- Start of the problem, the print script works
		print("Loading.. Package (".. playerid, "Time: " .. player.leaderstats.Time.Value ..")")
		playerdata:SetAsync(playerid, player.leaderstats.Time.Value)
		-- This part doesn't work, this should print when its done with setting the value, if this doesn't print it means it hasn't finished setting the value.
		print("success!")
	end)
	if not success then
		warn("Didn't save :(")
	end
end)

2 comments

0

Well, if you increase the value of "Time" in a LocalScript only you see that difference but the server doesn't. Therefore, the server sees it as 0, but you see it as a different value

0

i suggest you to use datastore2. it's so easy to use