I Do Not Know How To Finish My Script SaveData

Bruh, just use this parameter that’s given to you inside the for loop

This did not work. (Srry for the very late response @Jackscarlett )

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Value)
	end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Error When Saving Data")
		warn(errormessage)	
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Value)
		end)

		if success then
			print("Player Data Successfully Saved when the game was about to shutdown!")
		else	
			print("There Was An Erroer When Saving Data")
			warn(errormessage)	
		end
	end

end)

H o w t h o u g h

Did you get any Output prints?

No I Did Not. No errors either

I find that hard to believe that your Output did not print anything

Ok just in case, ARE API SERVICES ENABLED?

Very silly question, but is the script activated?
Is there a WaitForChild, loop or whatever makes the script wait?

@SOTR654 here is the script
local dss = game:GetService(“DataStoreService”)
local myDataStore = dss:GetDataStore(“myDataStore”)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Value)
	end)

	if success then
		print("Player Data Successfully Saved!")
	else	
		print("There Was An Erroer When Saving Data")
		warn(errormessage)	
	end
end)

game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-bigness", player.leaderstats.Bigness.Value)
		end)

		if success then
			print("Player Data Successfully Saved when the game was about to shutdown!")
		else	
			print("There Was An Erroer When Saving Data")
			warn(errormessage)	
		end
	end

end)

Ok I don’t know what you’re doing, but try this:

local DataService = game:GetService("DataStoreService")
local DataName = DataService:GetDataStore("SongStats")

local function LoadData(Player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = Player
	
	local Bigness = Instance.new("NumberValue")
	Bigness.Name = "Bigness"
	Bigness.Parent = stats
	
	local Data
	local success, whoops = pcall(function()
		Data = DataName:GetAsync(Player.UserId)
	end)
	
	if success and Data then
		print("Yes")
		Bigness.Value = Data
	else
		print("No")
		warn(whoops)
	end
end

local function SaveData(Player)
	local success, whoops = pcall(function()
		DataName:SetAsync(Player.UserId, Player.leaderstats.Bigness.Value)
	end)
	
	if success then
		print("Yes")
	else
		warn(whoops)
		print("H o w")
	end
end

game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)

game:BindToClose(function()
	for _, Player in pairs(game.Players:GetPlayers()) do
		SaveData(Player)
	end
end)

This literally worked for me and if it doesn’t work for you then idk what else to say

should this go in the same script?? (If yes then leaderstat or savedata)

It’s the same script, also if you’re using another script to call that GetAsync function remove that one

local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("myDataStore")

local function saveData(player)
xpcall(function()
myDataStore:UpdateAsync(
player.UserId .. "-bigness", 
function()
return player.leaderstats.Bigness.Value
end)
end, 
warn
)
end

local function playerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
	
local bigness = Instance.new("IntValue")
bigness.Name = "Bigness"
bigness.Parent = leaderstats

xpcall(function()
bigness.Value = myDataStore:GetAsync(player.UserId .. '-bigness')
end, warn)
end

game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
saveData(player)
end
end)

This Worked! For When I Left And For When I Xed Out! Thx Man!