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