"value of type nil cannot be converted to a number - Server - Leaderstats:31"

Hello! I have been getting issues with datastores on my leaderboard!
The issue ive been getting is:

  14:44:10.740  value of type nil cannot be converted to a number  -  Server - Leaderstats:31
  14:44:10.740  Stack Begin  -  Studio
  14:44:10.740  Script 'ServerScriptService.MainGameService.Leaderstats', Line 31  -  Studio - Leaderstats:31
  14:44:10.740  Script 'ServerScriptService.MainGameService.Leaderstats', Line 29  -  Studio - Leaderstats:29
  14:44:10.740  Stack End  -  Studio

And ive tried to fix it but it just doesnt work, Bassicly it detects if a player has already got data on his account and then it sets the data, and i keep getting errors because of it!

I cant fix it so heres the code

local plr
local char

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

game.Players.PlayerAdded:Connect(function(playeradded)
	
	plr = playeradded
	
	local LeaderstatsFolderInstance = Instance.new("Folder", playeradded)
	LeaderstatsFolderInstance.Name = "leaderstats"
	
	local MoneyValueInstance = Instance.new("NumberValue", LeaderstatsFolderInstance)
	MoneyValueInstance.Name = "Money"
	
	MoneyValueInstance:GetPropertyChangedSignal("Value"):Connect(function()
		
		ds:SetAsync(playeradded.UserId, MoneyValueInstance.Value)
		
	end)
	
	if not ds then
		
		ds:SetAsync(playeradded.UserId, 100)

	end
	
	local success, fail = pcall(function()
		
		MoneyValueInstance.Value = ds:GetAsync(plr.UserId)
		
	end)
	
	if success then
		
		print("Success")
		
	elseif fail then
		
		plr:Kick("Failed to load your data, Please jojn again :(")
		
	end
	
	
	plr.CharacterAdded:Connect(function(plrcharacteradded)
		
		char = plrcharacteradded
		
	end)
	
end)


game.Players.PlayerRemoving:Connect(function(playerremoved)
	
	ds:SetAsync(playerremoved.UserId, playerremoved:FindFirstChild("leaderstats"):FindFirstChild("Money").Value)
	
	playerremoved.CharacterRemoving:Connect(function(plrcharacterremoved)
		
		
		
	end)
	
end)

anyone who tries to help: thanks because i cant do it myself :skull:

1 Like

This getasync isn’t returning anything, this is likely due to the player not having any saved data, you could store the getasyncs value in a variable and check if it returns anything and what. You should also do a print just to check

local value = ds:GetAsync(plr.UserId)
print(value, type(value))

I also want to point out that you should not do this, it’s going to ratelimit the datastore since you’re updating every time you get money. Instead only update it when the player leaves.

it spits out “nil nil” everytime

this means that the data store doesn’t have that key registered. If a new player joins, someone that has never played before, they won’t have any data at all. So to fix this you need to check the data

local data = ds:GetAsync(plr.UserId)

if (data == nil) then --using == nil in case you ever set the data to false. So just to make sure they actually don't have data
 data = 0
end

MoneyValueInstance.Value = data

something like this

1 Like

Noted that you are using “NumberValue” which is only for floating point numbers. Change that to “IntValue” should fix it.

1 Like

Lua automatically turns strings, ints and floats to what you’re trying to use. They could save the value as a string and load that onto the number value if they really wanted to, and it would work fine. The only difference between int and float value are, intvalues can not have decimals but numbervalues can, but they will work fine without decimals

1 Like