This script is supposed to save and load a player's data upon joining and leaving the game.
For some reason, the script works in one place, but in all the other places I've tested it in, it hasn't worked.
I've gotten this output from playing it in-game:
Argument 2 is missing or nil Stack begin Script 'ServerScriptService.data', Line 15 - upvalue LoadPlayerData Script 'ServerScriptService.data', Line 42 Stack End
Can someone please help me?
Here's the script:
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local PlayersDataStore = DataStores:GetDataStore("Players")
local function SavePlayerData(Player)
if Player then
local Leaderstats = Player:FindFirstChild("perm")
if Leaderstats then
local XPValue, RankValue, HeistsValue, PrestigeValue
XPValue = Leaderstats:FindFirstChild("XP")
RankValue = Leaderstats:FindFirstChild("Rank")
HeistsValue = Leaderstats:FindFirstChild("Heists")
PrestigeValue = Leaderstats:FindFirstChild("Prestige")
if XPValue and RankValue and HeistsValue and PrestigeValue then
local PlayerDS = PlayersDataStore:GetAsync(Player.UserId)
if PlayerDS then
local SavingTable = {
["XP"] = XPValue.Value,
["Rank"] = RankValue.Value,
["Heists"] = HeistsValue.Value,
["Prestige"] = PrestigeValue.Value
}
print("saved " .. Player.Name .. "'s leaderstats")
PlayersDataStore:SetAsync(Player.UserId, SavingTable)
end
end
end
end
end
local function LoadPlayerData(Player, XPValue, RankValue, HeistsValue, PrestigeValue)
if Player then
local PlayerDS = PlayersDataStore:GetAsync(Player.UserId) -- this will return the 'SavingTable'
if PlayerDS then
XPValue.Value = PlayerDS.XP
RankValue.Value = PlayerDS.Rank
HeistsValue.Value = PlayerDS.Heists
PrestigeValue.Value = PlayerDS.Prestige
print("loaded " .. Player.Name .. "'s leaderstats")
else
print("new player")
PlayersDataStore:SetAsync(Player.UserId, nil)
end
end
end
local function SetupPlayer(Player)
--create leaderstats folder
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "perm"
Leaderstats.Parent = Player
--create value function
local function createNewValue(valueName)
local newValue = Instance.new("IntValue")
newValue.Name = valueName
newValue.Parent = Leaderstats
return newValue
end
--create values
local XPValue, RankValue, HeistsValue, PrestigeValue
XPValue = createNewValue("XP")
RankValue = createNewValue("Rank")
HeistsValue = createNewValue("Heists")
PrestigeValue = createNewValue("Prestige")
--load data
LoadPlayerData(Player, XPValue, RankValue, HeistsValue, PrestigeValue)
--autosaving
spawn(function()
while Player do
wait(10)
if Player then
SavePlayerData(Player)
end
end
end)
end
Players.PlayerAdded:Connect(SetupPlayer)
Players.PlayerRemoving:Connect(function(Player)
SavePlayerData(Player)
end)

5 comments
Good job! No line 107!
my bad - had an old script in the comments above it i didn't feature in this. oops.
Why there is a nil argument on Line 42?
Do you have the API service enabled? If not then datastores won't work.
The nil argument is there if there is no data load - i.e. make the script give the player give new data stats. And yes, the API service IS enabled, but it still does not work.