local function playerAdded(player)
local success, data = pcall(function ()
return StatStore:GetAsync(player.UserId)
end)
if success then
if data then
StatService.StatCache[player.UserId] = data
else
StatService.StatCache[player.UserId] =
{["Rebirths"] = 0,
["Speed"] = 0,
["Level"] = 0,
["Points"] = 0,
["Experience"] = 0
}
end
else
-- Handle case of error; kick, block saving, etc.
warn(string.format("Could not load data for %s: %s", player.Name, data))
end
print(StatService.StatCache)
end
That’s it, that’s the entire script, there’s no reason why it should even run, yet it returns an error because player is nil
Are you calling the playerAdded function anywhere else inside the script? The thing is that if you’re accidentally calling the function without even knowing it, that could be the potential reason why
no, i defined some variable previously, it’s a server script, but even weirder, I just tried running it, and it showed no error, i tried plaing the playeradded event and it returns an error when i run it despite no player being added, here is the script:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local StatStore = DataStoreService:GetDataStore("PlayerStats")
local StatService = require(game.ReplicatedStorage.Modules.StatService)
local function playerAdded(player)
local success, data = pcall(function ()
return StatStore:GetAsync(player.UserId)
end)
if success then
if data then
StatService.StatCache[player.UserId] = data
else
StatService.StatCache[player.UserId] =
{["Rebirths"] = 0,
["Speed"] = 0,
["Level"] = 0,
["Points"] = 0,
["Experience"] = 0
}
end
else
-- Handle case of error; kick, block saving, etc.
warn(string.format("Could not load data for %s: %s", player.Name, data))
end
print(StatService.StatCache)
end
local function playerRemoved(player)
print(player)
local success, data = pcall(function ()
return StatStore:SetAsync(player.UserId , StatService.StatCache[player.UserId])
end)
if not success then
-- Handle case of error; kick, block saving, etc.
warn(string.format("Could not load data for %s: %s", player.Name, data))
end
end
Players.PlayerRemoving:Connect(playerRemoved())
Players.PlayerAdded:Connect(playerAdded())