I was scripting and I came across a problem... This is my script

local DataStoreService = game:GetService("DataStoreService") local playerData = DataStoreService:GetDataStore("PlayerData")

local playerUserId = "Player_" .. player.UserId 
local data = playerData:GetAsync(playerUserId)  
if data then
 
    Win.Value = data
else

    Win.Value = 0
end

local function onPlayerExit(player)

local success, err = pcall(function()
    local playerUserId = "Player_" .. player.UserId
    playerData:SetAsync(playerUserId, player.leaderstats.Win.Value) 
end)

if not success then
    warn('Unable to save data')
end

end

game.Players.PlayerAdded:Connect(onPlayerJoin) game.Players.PlayerRemoving:Connect(onPlayerExit)

I'm trying to make it so it saves the players leaderstats, but it says ( 18:37:29.776 - ServerScriptService.SaveData:5: attempt to index nil with 'UserId') Anyone got an idea how to fix it?

2 comments

0

you're attempting to access localPlayer on the server.

0

For the function onPlayerJoin, is there "player"? ex. local function onPlayerJoin(player) or function onPlayerJoin(player)