Getting an error 'Attempt to index nil with userId'

yes, You need to enable “Studio access to api settings” in the game settings tab

Problem is that it is turned on

Are you using BindToClose?

If not, and you are the only player, that might be the very reason why.

what you mean? so if theres player , then it will work?

If you call the saveFunction for your data within BindToClose, the game server will “stay open” for about 2 minutes, whilst it runs the function.

It is great when unexpected crashes occur.

i get it , btw im having trouble with attempt to index nil with userid cause of bind2close
had no clue , any reason? maybe im the only player or , i played on studio? Nvm fixed

bindToClose doesn’t pass the player as an argument.

Use a “for loop”, and iterate through each of the players within the bindToClose function

local function saveData(plr)
	local didSave = false
	
	local success, errormessage = pcall(function()
		dataStore:SetAsync(plr.UserId.."-Money", plr.leaderstats.Money.Value)
		print("Money Data Saved from game shutdown/crashed")
	end)

	if success then
		didSave = true
		print("Successfully saved Player Data!")
	else
		print("There was an error while saving player data!")
		warn(errormessage)
	end
	
	return didSave
end

game.Players.PlayerRemoving:Connect(function(plr)
	saveData(plr)
end)

game:BindToClose(function(plr)
	for _, v in pairs(game.Players:GetPlayers()) do
		local didSave = saveData(v)
		if not didSave then
			warn("Player data failed to save.")
		end
	end
end)

i see , thanks for your workhard ill try it