DataStore Error

Hey, anyone knows how to fix this datastore bug?

heres the output

ServerScriptService.PlayerData:56: attempt to index nil with ‘UserId’ - Server - PlayerData:56

-- local function save(player)
	local userID = player.UserId --Line number 56
	local key = "Player_"..userID
	
	local food = player.leaderstats.Food.Value
	local coins = player.leaderstats.Coins.Value
	
	local dataTable = {
		Food = food,
		Coins = coins
	}
	print(dataTable)
	
	local success,returnValue
	success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
		return dataTable
	end)
	
	if success then
		print("Data saved")
	else
		print("Data Saving error")
	end
end
local function onShutDown()
	if RunService:IsStudio() then
		wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers
		
		for _, player in ipairs(allPlayers) do
			coroutine.wrap(function()
				save(player)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)()
		end
		finished.Event:Wait()
	end
end
Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save())
game:BindToClose(onShutDown)

if need heres the full script

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")

local dataStore = DataStoreService:GetDataStore("Test") --Always set to "Data" when public
--local function givePlayerCurrency(player: player)
	--while true do
		--	wait(1)
		--	player.leaderstats.Food.Value += 1
		--player.leaderstats.Coins.Value += 1
		--end
--end

local function setupPlayerData(player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local food = Instance.new("IntValue", leaderstats)
	food.Name = "Food"
	food.Value = 0
	
	local coins = Instance.new("IntValue", leaderstats)
	coins.Name = "Coins"
	coins.Value = 0
	
	local success, returnValue
	success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
	
	if success then
		if returnValue == nil then
			returnValue = {
				Food = 0,
				Coins = 0,
			}
		end
		
		print(returnValue)
		food.Value = if returnValue.Food ~= nil then returnValue.Food else 0
		coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
		
	else
		player:Kick("Error loading your Data! Roblox datastore might be down, TRy again later, or contact us through the group!")
		print(player.Name.."Data loading error")
		
	end
	
	--givePlayerCurrency(player)
	

end

local function save(player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local food = player.leaderstats.Food.Value
	local coins = player.leaderstats.Coins.Value
	
	local dataTable = {
		Food = food,
		Coins = coins
	}
	print(dataTable)
	
	local success,returnValue
	success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
		return dataTable
	end)
	
	if success then
		print("Data saved")
	else
		print("Data Saving error")
	end
end
local function onShutDown()
	if RunService:IsStudio() then
		wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers
		
		for _, player in ipairs(allPlayers) do
			coroutine.wrap(function()
				save(player)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)()
		end
		finished.Event:Wait()
	end
end
Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save())
game:BindToClose(onShutDown)

Maybe you need to change line 100 to

Players.PlayerRemoving:Connect(save)

instead of

Players.PlayerRemoving:Connect(save())
2 Likes

Thank you so much!!!

2 Likes