Error: Value of type nil cannot be converted to a number

Please help me solve the problem.
Error: Value of type nil cannot be converted to a number.
The script:

local dataStoreService = game:GetService("DataStoreService")
local stageStore = dataStoreService:GetDataStore( "PlayerStageDataStore")

function plrToStage(plr)
	repeat wait() until plr.Character.HumanoidRootPart
	local stagePart = game.workspace.Checkpoints:FindFirstChild(tostring(plr.leaderstats.Stage.Value))
	if stagePart then
		plr.Character.HumanoidRootPart.CFrame = stagePart.CFrame + Vector3.new(0,2.5, 0)
	else
		warn("Checkpoint " .. tostring(plr.leaderstats.Stage.Value) .. " not found.")
	end
end

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

	local stage = Instance. new("IntValue")
	stage.Name = "Stage"
    stage.Parent = leaderstats


    local success, errorMsg = pcall(function()
	     stage.Value = stageStore:GetAsync(player.UserId)
    end)

     if success then
	     print("Successfully got "..player.Name.."'s stage data.")
     else
	     warn(errorMsg)
	 end
	 
	 if player.Character then
		plrToStage(player)
	 end
	 
	 player.CharacterAdded:Connect(function()
		plrToStage(player)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errorMsg = pcall(function()
		stageStore:SetAsync(player.UserId,player.leagerstats. Stage.Value)
	end)

    if success then
	     print("Successfully saved "..player.Name.."'s stage data.")
    else
	     warn(errorMsg)
	end
end)

for _, checkpoint in pairs(game.Workspace.Checkpoints:GetChildren()) do
	checkpoint.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			local checpointNumber = tonumber(checkpoint.Name)
			if player.leaderstats.Stage.Value < tonumber(checkpoint.Name) then
				player.leaderstats.Stage.Value = tonumber(checkpoint.Name)
			end
		end
	end)
end

it was written somewhere that there is such a bug, but not in roblox studio Save Data also does not work.
I will be grateful to everyone for their help.

1 Like

What line does the error occur on? you can just send a screenshot of the entire error

2 Likes

Also regarding your second issue, regarding saving:
You’ve written player.leagerstats instead of player.leaderstats on line 44

3 Likes

Oops. How could I not notice. but by the way, it really helped. although the error itself did not disappear, the script started working as it should. thank you.

2 Likes

It looks like the error was happening where you were trying to call a :GetAsync function.
To remove that error, just add “stage.Value = 1” on line 22, so the value isn’t nil.

2 Likes

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