Need help with datastores

so im making a game that saves points as paper and im making my datasave but i keep getting this error

  14:03:58.483   ▶ ServerScriptService. PointSetup:22: attempt to index nil with 'UserId' (x4)  -  Server -  PointSetup:26
  14:04:10.544  ServerScriptService. PointSetup:41: attempt to index nil with 'UserId'  -  Server -  PointSetup:41

my code is

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

local database = DataStoreService:GetDataStore("data")
local sessionData = {}

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Paper = Instance.new("IntValue")
	Paper.Name = "Paper"
	Paper.Value = 0
	
	local success = nil
	local player = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
		return	database:GetAsync(player.UserId)
		end)
		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5
	if success then
		print("Connected to database")
		if not playerData then
			print("Assigning default data")
			playerData = {
				
				["Paper"] = 0
				
			}
		end
		sessionData[player.UserId] = playerData
	else warn("Unable to get data for", player.UserId)
		player:kick("Unable to load your data. Try again later")
	end
	
	Paper.Parent = leaderstats
end

Players.PlayerAdded:Connect(onPlayerAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success
		local errorMsg = nil
		local attempt = 1
	repeat
		success, errorMsg = pcall(function()
			database:SetAsync(player.UserId, sessionData[player.UserId])
		end)
		attempt += 1
		if not success then
			warn(errorMsg)
			task.wait(3)
		end
	until success or attempt == 5	
	
	if success then
		print("Data saved for", player.Name)
	else
		warn("unable to save for", player.Name)
	end
	end
end
Players.PlayerRemoving:Connect(PlayerLeaving)




game.Workspace.count.Touched:connect(function(hit)
	if hit.Name == "Paper" then -- change this
			
			local playerList = Players:GetPlayers()
			for currentPlayer = 1, #playerList do
				local player = playerList[currentPlayer]
				local Paper = player.leaderstats.Paper
				Paper.Value += 1
		end
	end
end)

help is appreciated

So you get player from onplayeradded which looks good, but then you seem to make a mistake where you make a new local variable called player to nil, which overwrites the player variable. So you need to remove local player = nil

Edit: seems you meant to set playerData to nil (not player) considerinf you setting success to nil

5 Likes

As @odin12458 mentioned your overwriting the initial player variable within the scope of the playerAdded function with a new local variable player with the value nil.

2 Likes

what player data in the code do i set to nil i made it so player is fixed like so

local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Paper = Instance.new("IntValue")
	Paper.Name = "Paper"
	Paper.Value = 0
	
	local success = nil
	local player
	local attempt = 1

you are setting another variable “player” in same envirement
That the entire problem here bro :skull:
People already pointed to that;
If you want it to be local in a new envirement please do (ironically keyword is also do xd)

do
local player:Player? = nil
--blah blah blah dunno what you want to archive this way
end
2 Likes

:skull:
Man…You are overriding return value…

1 Like