Data store error

I have been getting this error since I have been using data store and it is very annoying.

This is the error:
13:20:05.964 - ServerScriptService.DataStore:134: attempt to index nil with ‘Cash’

Here is part of the script:

local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "Leaderstats"
	Leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = Leaderstats

local success, errormessage = pcall(function()
		data = DataStore1:GetAsync(playerUserId)
	end)
	
	if success then
           Cash.Value = data.Cash
        --Other values are here
    end

local data = {Cash = player.Leaderstats.Cash.Value;
--Other values are here 
}

Could you provide the full script?

Are you sure that you’re using DataStoreService Correctly?

Its is only part of it and I am sure I am using it correctly.

I can’t really tell if you are using it correctly or not since I cant see what you’re referencing.

In short, if you’re only saving one value you should do so without using an array.

Try printing data and data.Cash to see what its returning.

data = DataStore1:GetAsync(playerUserId) is stored in an array. You have to index it using numerical form.

another issue is, data is defined after you tried setting the value.

For some reason it works now…

I honestly don’t know why this always happens.

Edit: I don’t get the error but I doesn’t save.

That error normally happens to a lot of people when the leaderstats dont load very quickly but its an error you shouldnt worry about