How do i fix the "value cannot be converted to a number" error?

Hello, i am a beginner scripter in roblox studio, but im learning new things really fast.

I had that one error in Roblox Studio where i was making a data store in my game (The Ultimate Elevator) and i kept getting an error like this:
image

I really don’t know how to fix it, i even tried converting the value to tonumber(), nothing worked and gave me another error, and this is my script i made:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("ElevatorLevel")

game.Players.PlayerAdded:Connect(function(player)

	local leaderstats = player:FindFirstChild("hiddenLeaderstats")

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

	local data
	local success, errormessage = pcall(function()
		data = ds:GetAsync(player.UserId.."-ElevatorLevel")
	end)

	if success then
		ElevatorLevel.Value = data -- This is the line where the error appeared.
	else
		warn(errormessage)
		print("error loading data")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)

	local success, errormessage = pcall(function()
		ds:SetAsync(player.UserId.."-ElevatorLevel", player:WaitForChild("hiddenLeaderstats").ElevatorLevel.Value)
	end)

	if success then
		print("player data saved.")
	else
		print("failed to save data")
		warn(errormessage)
	end
end)
2 Likes

For first time players data will be nil so you are trying to set the value to nil. Try checking if the data exists before doing that

if success and data then
		ElevatorLevel.Value = data -- This is the line where the error appeared.
1 Like

It actually printed “nil” as a warning and nextly “error loading data”, so the Value was not equal to data.