"value of type nil cannot be converted to a number" on datastore script

local DataStoreService = game:GetService("DataStoreService")
local PlayerInventory = DataStoreService:GetDataStore("PlayerInventory")
local PlayerCoins = DataStoreService:GetDataStore("PlayerInventory", "Coins")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	local coins = Instance.new("IntValue", leaderstats)
	coins.Name = "Coins"
	local success, currentcoins = pcall(function()
		return PlayerCoins:GetAsync(plr.UserId)
	end)
	if success then
		coins.Value = currentcoins
	else
		coins.Value = nil
	end
end)


How should I go about fixing this? Should I add a playerRemoving function so it saves the data when the player leaves the game?

1 Like

The property Value of the IntValue only accepts numbers, in this line you trying to set the value to nil, just set it to zero, should work.

if success and currentcoins ~= nil then
	coins.Value = currentcoins
else
	coins.Value = 0
end

Also, I don’t recommend saving the DataStore value directly from the IntValue property. Since the IntValue is parented inside leaderstats, and leaderstats is parented inside the Player, everything inside the player is automatically destroyed when the player leaves. This means that if there’s any delay or if the DataStore call takes too long when the player is leaving, the value might be lost before it’s saved.

1 Like

You are making an Int value so it doesn’t make sense for this be ever be nil., because nil is not a number. You can’t do this, and it should be coins.Value = 0 or if you have a different default integer value that should be set

You could also consider doing something like this:

coins.Value = tonumber(currentcoins) or 0

And the data will be corrupted.

Yeah, I had it set to 0 originally. I changed it to nil hoping it would fix it but obviously it did not lol

1 Like

nil is not a number and IntValue.Value only accept number type. just replace coins.Value = nil with coins.Value = 0 at line 17. (nil is like NULL pointer , 0 (for C/C++) not nothing. Luau treat it as an invalid internal type)
but I wanna give some advice:
Why dont you provide better failure handling? You should support the retry mechanism. Delivering your code into a bad production environment can cause data to not be available properly because :GetAsync sometimes fails and your practices do not comply with any common handling rules.

yeah im still starting out and I really don’t know what any of the code actually means, so I need to work on that. Thanks for the advice though

For new developer . exploring DataStoreService is a good option though. However, professional network logic is needed behind it to handle complex session conflicts. If you don’t plan to learn this type of knowledge, you can use community modules like DataStore2 (by Roblox) and ProfileStore ProfileService (by loleris) DocumentService (by Sublivion) Datakeep (by noahrepublic)

I fixed it. It was because I was incrementing my coin value using a LOCAL script so it only changed the value on the CLIENT and not the server, so the server recognized the coin value as 0. :sweat_smile:

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