Why do I keep getting this error?

I am trying to make an Obby game but I keep on getting this error.

  03:27:04.602  value of type nil cannot be converted to a number  -  Server - Leaderstats:26
  03:27:04.602  Stack Begin  -  Studio
  03:27:04.602  Script 'ServerScriptService.Leaderstats', Line 26  -  Studio - Leaderstats:26
  03:27:04.602  Stack End  -  Studio

I dont know why it keeps on doing this but here is the code:

local DatastoreService = game:GetService("DataStoreService")
local plrdata = DatastoreService:GetDataStore("plrdata")
local checkpointsfolder = game.Workspace.Ceckpoints


game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local Stage = Instance.new("IntValue", leaderstats)
	Stage.Name = "Stage"
	Stage.Value = 1
	
	local Deaths = Instance.new("IntValue", leaderstats)
	Deaths.Name = "Deaths"
	Deaths.Value = 0
	
	local data
	local userid = plr.UserId
	local success, ermsg = pcall(function()
	data = plrdata:GetAsync(userid)
	end)
if success then
	if data then
		Stage.Value = data.Stage
    	Deaths.Value = data.Deaths
	end
else
	print("Could Not Find Data")
end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {Stage = plr.leaderstats.Stage.Value ;
		Deaths = plr.leaderstats.Deaths.Value ;
	}
	local userid = plr.UserId
local success, ermsg = pcall(function()
	plrdata:SetAsync(userid, data)
end)
	if success then
		print("Data Saved")
	else
		print("Data not Saved")
	end
end)


Do these print?

There could be a couple of reasons why the data is nil:

  • It’s a player’s first time playing the game.
  • The game did not have enough time to save the data before closing down.

You should:

  • Add a BindToClose function to save data on game closing
  • Check if data is nil and assign default data if it is

Okay so the data is a table and in the table the Deaths value is nil.

Have you tried to leave the game and join again ?

Or also try this, I don’t think that will change anything but try :

local DatastoreService = game:GetService("DataStoreService")
local plrdata = DatastoreService:GetDataStore("plrdata")
local checkpointsfolder = game.Workspace.Ceckpoints


game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local Stage = Instance.new("IntValue", leaderstats)
	Stage.Name = "Stage"
	Stage.Value = 1

	local Deaths = Instance.new("IntValue", leaderstats)
	Deaths.Name = "Deaths"
	Deaths.Value = 0

	local data
	local userid = plr.UserId
	
	local success, ermsg = pcall(function()
		data = plrdata:GetAsync(userid)
	end)
	
	if success then
		if data then
			Stage.Value = data.Stage
			Deaths.Value = data.Deaths
		end
	else
		print("Could Not Find Data")
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local data = {
		["Stage"] = plr.leaderstats.Stage.Value,
		["Deaths"] = plr.leaderstats.Deaths.Value,
	}
	
	local userid = plr.UserId
	
	local success, ermsg = pcall(function()
		plrdata:SetAsync(userid, data)
	end)
	
	if success then
		print("Data Saved")
	else
		print("Data not Saved")
	end
end)

How do I add those? By the way yes it does print

To add data checking, when you load the data, you could just do:

if data == nil then
    data.Stage = 1
    data.Deaths = 0
end

And a BindToClose function:

--you need to convert your saving function to this format:
local function save(player)
    --saving code goes in here
end

game:BindToClose(function()
    if game["Run Service"]:IsStudio() then task.wait(3) return nil end
    for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
        save(player)
    end
    task.wait(3)
end)
1 Like

Yo thank you so much it worked

1 Like

Don’t forget to check solution for the reply that helped you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.