SECTION IS NOW Closed

when i went to rename the data Store, I noticed this error that happens when a user doesn’t have any data stored, it said "ServerScriptService.RobloxWorld_DataStore:37: attempt to index nil with ‘CheckPoint’ "

local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local saveDataStore = dataStoreService:GetDataStore(“CheckPoints7”)

local function savePlrData(plr)
local success,err = pcall(function()

  local saveData = {}
  for _,stat in pairs(plr.leaderstats:GetChildren()) do
  	saveData[stat.Name] = stat.Value
  end
  saveDataStore:SetAsync(plr.UserId,saveData)

end)
if not success then return err end
end

players.PlayerAdded:connect(function(plr)
local stats = Instance.new(“Folder”)
stats.Name = “leaderstats”
stats.Parent = plr

local CHECKPOINTS = Instance.new(“IntValue”)
CHECKPOINTS.Name = “CheckPoint”
CHECKPOINTS.Parent = stats
CHECKPOINTS.Value = 1

local success, currentData = pcall(function()
return saveDataStore:GetAsync(plr.UserId)
end)

if success then

  for _,stat in pairs(stats:GetChildren()) do
  	stat.Value = currentData[stat.Name]

  end

else
print(plr.Name… " Has No Data")

end

local function TeleportToCheckpoint(chr, checkpoint)
if checkpoint ~= nil then
local part = workspace.CheckPoints:WaitForChild(checkpoint)
repeat wait() until chr.Humanoid
if chr.PrimaryPart ~= nil then
chr.PrimaryPart.CFrame = part.CFrame + Vector3.new(0,1,0)
end

  end

end

plr.CharacterAdded:connect(function(char)
if CHECKPOINTS.Value ~= nil then
local part = workspace.CheckPoints:WaitForChild(CHECKPOINTS.Value)
local PRIMPART = char:WaitForChild(“HumanoidRootPart”)
repeat wait() until PRIMPART ~= nil
if PRIMPART ~= nil then
PRIMPART.CFrame = part.CFrame + Vector3.new(0,1,0)
end

  end

end)

end)

players.PlayerRemoving:connect(function(plr)

local err = savePlrData(plr)
if err then print(err) end
end)

game:BindToClose(function()
for _,plr in pairs(players:GetPlayers()) do
local err = savePlrData(plr)
if err then print(err) end
end
wait(2)
end)

What is line 37? Also, going forward it is a very good idea to spend some time indenting your code.

for _,stat in pairs(stats:GetChildren()) do

Transpiled into a proper code block:


Couldn’t you just check whether the player’s retrieved data is nil?

if success and currentData ~= nil then
	-- loop through stats
else
	print(plr.Name .. " Has No Data")
end

I have solved the issue with this script!

1 Like