Is 0 Considered Nil?

Hi. I was creating a datastore system, and I noticed when I was saving, i got an error. Argument 2 missing or nil. I think this is because 0 is considered nil, because when I left the game, the value I was saving was 0. Is this true?

No, 0 isn’t nil. Nil is nothing, 0 is still something(an integer, or int).

5 Likes

That’s what i thought. Ill look into my script some more.

1 Like

I can’t seem to figure it out.

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Data3")

local UpdateGUI = game.ReplicatedStorage.UpdateGUI
local coinCollected = game.ReplicatedStorage.CoinCollected

local coins = 0

game.Players.PlayerAdded:Connect(function(player)
	local getData
	local success, err = pcall(function()
		getData = dataStore:GetAsync(player.UserId) 
	end)
	
	if success then
		coins = getData
		UpdateGUI:FireClient(player, coins)
	else
		coins = 0
		UpdateGUI:FireClient(player, coins)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	dataStore:SetAsync(player.UserId, coins)
end)

coinCollected.Event:Connect(function(player, coinModel)
	coinModel:Destroy()
	UpdateGUI:FireClient(player, coins)
end)
1 Like

It’s because if a player’s data doesn’t exist, you’re setting the value to nil.

if success then
    coins = getData or 0 -- it's set to getData, if getData doesn't exist, it gets set to 0
else
    coins = 0
    UpdateGUI:FireClient(player, coins)
end

You should also move your coins variable inside of the .PlayerAdded event.

local coins = 0

game.Players.PlayerAdded:Connect(function(player)
	local getData
	local success, err = pcall(function()
		getData = dataStore:GetAsync(player.UserId) 
	end)

to

game.Players.PlayerAdded:Connect(function(player)
        local coins = 0
	local getData
	local success, err = pcall(function()
		getData = dataStore:GetAsync(player.UserId) 
	end)
6 Likes

Like @VegetationBush stated, 0 is an integer. It’s a number and is equal to something. Nil means nothing, like nothing exists at all.

Moving the coins variable to the PlayerAdded event will prevent me from accessing it from the PlayerRemoving event

Wait- I forgot that I could just remove the local keyword

1 Like

You’d want to index it when the player leaves, the variable changes for everybody in the server.

Player1 joins; coins = 100

Player2 joins; coins = 0

Player1 leaves; their data is set to 0

Instead I recommend creating a value and then indexing it when the player leaves, or you could use a dictionary.

game:GetService('Players').PlayerAdded:Connect(function(player)
    -- your func to get data

    local coinsValue = Instance.new('IntValue')
    coinsValue.Name = 'Coins'
    coinsValue.Parent = player
    coinsValue.Value = getData

…

game:GetService('Players').PlayerRemoving:Connect(function(player)
    local coinsValue = player:FindFirstChild('Coins') -- that Coins value we created earlier
    local coins = coinsValue.Value
    local success, err = pcall(function() -- I strongly recommend wrapping it in a pcall to catch and handle any potential errors.
        dataStore:SetAsync(player.UserId, coins)
    end)
    if not success and err then
        warn(err)
    end
end)
1 Like

0 is considered nil in some instances. It also is another way of saying N/A values. Another one is nan which is an infinitely large & infinitely small value.

Wrong, for infinitely large or infinitely small values either inf or -inf is used. Nan means “Not a number” which is used when you perform an arithmetic operation that has it’s result either undefined or returns a number that program doesn’t recognize (Like complex numbers for example.).

Also can you give me a single valid example where 0 is considered nil?

Incorrect, 0 is an integer. Nil means nothing at all. 0 is an integer.