"value of type nil cannot be converted to a number" Error

I’m trying to make a datastore script but all of a sudden these “value of type nil cannot be converted to a number” errors are appearing.
Data is being found but it won’t be set onto the players statistics(such as cash) take a look at the script:

local Services = {
	ReplicatedStorage = game:GetService("ReplicatedStorage"),
	Players = game:GetService("Players"),
	UIS = game:GetService("UserInputService"),
	RunService = game:GetService("RunService"),
	DataService = game:GetService("DataStoreService"),
}

local default_Set = {
	Cash = 1500,
}

local CoreData = Services.DataService:GetDataStore("CoreData_010l")
local storeName = "data_"

local function onPlayerJoined(player:Player)
	local Cash = Instance.new("NumberValue", player)
	Cash.Name = "Cash"
	local storeCashes = Instance.new("StringValue",Cash)
	storeCashes.Name = storeName
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Reputation = Instance.new("NumberValue",leaderstats)
	local Wanted = Instance.new("NumberValue",leaderstats)
	Reputation.Name = "Rep."
	Wanted.Name = "Wanted"
	local storeRep = Instance.new("StringValue",Reputation)
	storeRep.Name = storeName
	local storeWanted = Instance.new("StringValue",Wanted)
	storeWanted.Name = storeName
	
	local playerID = "Player_"..player.UserId
	local data = CoreData:GetAsync(playerID)
	
	if data then
		print("Data found for this player, setting...")
		Cash.Value = data["Cash"]
		Reputation.Value = data["Reputation"]
		Wanted.Value = data["Wanted"]
	else
		print("No data found for this player.")
		Cash.Value = default_Set.Cash
	end
end

local function createSession(player:Player)
	local plr_data = {}
	for _, v in ipairs(player:GetDescendants()) do
		if v:FindFirstChild(storeName) then
			plr_data[v.Name] = v.Value
		end
		return plr_data
	end
end

local function onPlayerLeft(player:Player)
	local plr_data = createSession(player)
	local success, err = pcall(function()
		local userID = 'Player_'..player.UserId
		CoreData:SetAsync(userID, plr_data)
	end)
	
	if not success then
		warn("Unable to save session.")
	end
end

Services.Players.PlayerAdded:Connect(onPlayerJoined)
Services.Players.PlayerRemoving:Connect(onPlayerLeft)

Hi
change all “NumberValue” to “IntValue”

1 Like

Something is not being found. That is what nil means.

So you cannot apply a function to a nil (nothing).

Looks at the line of code that the error message indicates.

Something on that line is nil so the code cannot continue.

1 Like

What line are you getting the error on? If data is being found, then print that and see which one’s nil

or try this:

local function createSession(player: Player)
local plr_data = {}
for _, v in ipairs(player:GetDescendants()) do
if v:IsA(“NumberValue”) or v:IsA(“StringValue”) then
plr_data[v.Name] = v.Value
end
end
return plr_data
end

I got the error for each of these lines:

print("Data found for this player, setting...")
		Cash.Value = data["Cash"]
		Reputation.Value = data["Reputation"]
		Wanted.Value = data["Wanted"]

ok print data before trying to set them

Don’t think this will solve the issue, I had the if v:FindFirstChild() there so it would only save values with something specific inside them

Screen capture the actual error message.

The first one that appears is the most important. Many that follow are just caused by the first error. So screen capture them all and post here.

The data table is just empty, I don’t know why though

Ah, i got it, in your createSession function, you are returning after 1 iteration, take your return outside of the for loop

local function createSession(player:Player)
	local plr_data = {}
	for _, v in ipairs(player:GetDescendants()) do
		if v:FindFirstChild(storeName) then
			plr_data[v.Name] = v.Value
		end
	end
	return plr_data
end
2 Likes

Replace the block of code with this one that has print commands:

	if data then
		print("Data found for this player, setting...")
		
		print("Cash.Value =", Cash.Value)
		print("Reputation.Value =", Reputation.Value)
		print("Wanted.Value =", Wanted.Value)
		
		Cash.Value = data["Cash"]
		Reputation.Value = data["Reputation"]
		Wanted.Value = data["Wanted"]
	else
		print("No data found for this player.")
		Cash.Value = default_Set.Cash
	end
1 Like

No need, their values will be 0 as you are setting them afterwards?

The errors are still appearing…

print plr_data when leaving, check if they are nil or not

Got these:

  18:39:33.553  Data found for this player, setting...  -  Server - NewData:38
  18:39:33.554  Cash.Value = 0  -  Server - NewData:40
  18:39:33.554  Reputation.Value = 0  -  Server - NewData:41
  18:39:33.554  Wanted.Value = 0  -  Server - NewData:42
  18:39:33.554  value of type nil cannot be converted to a number  -  Server - NewData:45
  18:39:33.555  Script 'ServerScriptService.DS.NewData', Line 45 - function onPlayerJoined

Is Line 45 =:

	`Reputation.Value = data["Reputation"]`

In your script?

Yes, it is:

	
	if data then
		print("Data found for this player, setting...")

		print("Cash.Value =", Cash.Value)
		print("Reputation.Value =", Reputation.Value)
		print("Wanted.Value =", Wanted.Value)

		Cash.Value = data["Cash"]
		Reputation.Value = data["Reputation"]
		Wanted.Value = data["Wanted"]
	else
		print("No data found for this player.")
		Cash.Value = default_Set.Cash
	end
end

The issue isn’t with your PlayerAdded event, it’s with PlayerRemoving. Print plr_data when leaving.

Doesn’t seem to be printing it, this is what I added:

local function onPlayerLeft(player:Player)
	local plr_data = createSession(player)
	local success, err = pcall(function()
		local userID = 'Player_'..player.UserId
		CoreData:SetAsync(userID, plr_data)
	end)
	
	if not success then
		warn("Unable to save session.")
	end
	print(plr_data)
end

I also tried printing it inside of the local pcall function