Attempt to index nil with 'Rebirths'

Hi there, im trying to make a rebirth saving system, but it gives me this errors:

Code:

-- Leaderstats

local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = 'leaderstats'
	
local Rebirths = Instance.new('NumberValue', Leaderstats)
Rebirths.Name = 'Rebirths'
Rebirths.Value = 0
	
local PlayerValues = Instance.new('Folder', Player)
PlayerValues.Name = 'PlayerValues'
	
local Multiplier = Instance.new('IntValue', PlayerValues)
Multiplier.Name = 'Multiplier'
Multiplier.Value = 1
	
local RebirthCost =  Instance.new('IntValue', PlayerValues)
RebirthCost.Name = 'RebirthCost'
RebirthCost.Value = 3

-- Data store script
local DataStoreService = game:GetService('DataStoreService')
local RebirthsDataStore = DataStoreService:GetDataStore('RebirthsDataStore')

game.Players.PlayerAdded:Connect(function(Player)
	local leaderstats = Player:WaitForChild('leaderstats')
	local Rebirths = leaderstats.Rebirths
	
	local Multiplier = Player.PlayerValues:WaitForChild('Multiplier')
	local RebirthCost = Player.PlayerValues:WaitForChild('RebirthCost')
	
	local PlayerUserId = 'Player_'..Player.UserId
	
	local data
	local success, errormessage = pcall(function()
		data = RebirthsDataStore:GetAsync(PlayerUserId)
		print(data)
	end)
	
	if success then
		Rebirths.Value = data.Rebirths
		Multiplier.Value = data.Multiplier
		RebirthCost.Value = data.RebirthCost
	else
		warn(errormessage)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local PlayerUserId = 'Player_'..Player.UserId
	
	local success, errormessage = pcall(function()
		RebirthsDataStore:SetAsync(PlayerUserId, {
			Rebirths = Player.leaderstats:WaitForChild('Rebirths').Value,
			Multiplier = Player:WaitForChild('PlayerValues').Multiplier,
			RebirthCost = Player:WaitForChild('PlayerValues').RebirthCost
		})
	end)
end)
3 Likes

printing data returns nil,

And this causing the error:

2 Likes

Just to clarify, printing Data returns nil, but then you attempt to index the nil value for Rebirths, Multiplier , RebirthCost?

edit:

	local Rebirths = leaderstats.Rebirths

Is this line 20 that has the indexed Rebirths error?

1 Like

yeah, i dont have experience in making datastores with this method btw

1 Like

@Extrenious this is the line with error

Random unnecessary chatter

I personally was disappointed to see that you were using leader stats. I never mentioned it because we all start and learn at our own pace. I have never experimented with interacting directly with datastores. Instead, I used datastore modules created by individuals with a deeper understanding.

Firstly, where is the Default DataTable for the data for new players?

In your case, it’s nil the exact event where you would use! a default data table instead of the nil data you have

1 Like

I have 2 datastores rn, but 1st seems to not save rebirths data, so I created this one

at this point ill just create another game with this system and will not add rebirths to the game. This feels overwhelming for me rn, cuz im trying to make a first game

Its okay just keep calm its fairly simple
don’t! let frustration stop you trust me… I’ve wasted a lot of time being frustrated even now avoiding development lol

edit : give me a moment ill try to solve this lil problem for you.
be bac

alr I can send leaderstats script too.

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local UpgradeSaveRemote = ReplicatedStorage.Events:WaitForChild('UpgradeSaveRemote')
local RebirthEvent = ReplicatedStorage.Events:WaitForChild('RebirthEvent')

local DataStoreService = game:GetService('DataStoreService')
local DataStore = DataStoreService:GetDataStore("DataStore")

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(Player)
	
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = 'leaderstats'
	
	local Freakiness = Instance.new('NumberValue', Leaderstats)
	Freakiness.Name = 'Freakiness'
	Freakiness.Value = 0
	
	local Rebirths = Instance.new('NumberValue', Leaderstats)
	Rebirths.Name = 'Rebirths'
	Rebirths.Value = 0
	
	local PlayerValues = Instance.new('Folder', Player)
	PlayerValues.Name = 'PlayerValues'
	
	local Multiplier = Instance.new('IntValue', PlayerValues)
	Multiplier.Name = 'Multiplier'
	Multiplier.Value = 1
	
	local RebirthCost =  Instance.new('IntValue', PlayerValues)
	RebirthCost.Name = 'RebirthCost'
	RebirthCost.Value = 3
	
	-- Upgrades saving
	local UpgradeStatus = Instance.new('Folder', Player)
	UpgradeStatus.Name = 'UpgradeStatus'
	
	local isUpgradeBought1 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought1.Name = 'Freaky cat'
	
	local isUpgradeBought2 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought2.Name = 'Freaky Bob'
	
	local isUpgradeBought3 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought3.Name = 'Alien freak'
	
	local isUpgradeBought4 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought4.Name = 'Eater'
	
	local isUpgradeBought5 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought5.Name = 'Freaky Freddy'
	
	local isUpgradeBought6 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought6.Name = 'Quincy freak'
	
	local isUpgradeBought7 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought7.Name = 'Mahito freak'
	
	local isUpgradeBought8 = Instance.new('BoolValue', UpgradeStatus)
	isUpgradeBought8.Name = 'Extra freaky Bob'
	
	
	UpgradeSaveRemote.OnServerInvoke = function(player, UpgradeValue, UpgradeName)

		Player.UpgradeStatus[UpgradeName].Value = UpgradeValue
		
		return Player.UpgradeStatus[UpgradeName].Value

	end
	
	--RebirthEvent.OnServerEvent:Connect(function(Player, RebSave, MultSave)

	--	local leaderstats = Player:WaitForChild('leaderstats')
	--	local Multiplier = Player.PlayerValues:WaitForChild('Multiplier')
	--	local RebirthCost = Player.PlayerValues:WaitForChild('RebirthCost')

	--	leaderstats.Freakiness.Value = 0
	--	leaderstats.Rebirths.Value += 1

	--	Multiplier.Value += 0.5
	--	RebirthCost.Value *= 2
	--	print('Rebirth cost server: '..RebirthCost.Value)
	--end)
	
	-- Data saving
	local DataToSave = DataStore:GetAsync(Player.UserId)
	
	if DataToSave then
		Freakiness.Value = DataToSave[1]
		
		isUpgradeBought1.Value = DataToSave[2]
		isUpgradeBought2.Value = DataToSave[3]
		isUpgradeBought3.Value = DataToSave[4]
		isUpgradeBought4.Value = DataToSave[5]
		isUpgradeBought5.Value = DataToSave[6]
		isUpgradeBought6.Value = DataToSave[7]
		isUpgradeBought7.Value = DataToSave[8]
		isUpgradeBought8.Value = DataToSave[9]
		
		--Rebirths.Value = DataToSave[10]
		--Multiplier.Value = DataToSave[11]
		--RebirthCost.Value = DataToSave[12]
		
	else
		
		local ValuesToSave = {
			Freakiness.Value,
			
			isUpgradeBought1.Value,
			isUpgradeBought2.Value,
			isUpgradeBought3.Value,
			isUpgradeBought4.Value,
			isUpgradeBought5.Value,
			isUpgradeBought6.Value,
			isUpgradeBought7.Value,
			isUpgradeBought8.Value,
			
			--Rebirths.Value,
			--Multiplier.Value,
			--RebirthCost.Value
		}
		DataStore:GetAsync(Player.UserId, ValuesToSave)
		
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	
	local Leaderstats = Player:WaitForChild('leaderstats')
	
	local Freakiness = Leaderstats:WaitForChild('Freakiness')
	--local Rebirths = Leaderstats:WaitForChild('Rebirths')
	--local Multiplier = Player.PlayerValues:WaitForChild('Multiplier')
	--local RebirthCost = Player.PlayerValues:WaitForChild('RebirthCost')
	
	local ValuesToSave = {
		Freakiness.Value,

		Player.UpgradeStatus:FindFirstChild('Freaky cat').Value,
		Player.UpgradeStatus:FindFirstChild('Freaky Bob').Value,
		Player.UpgradeStatus:FindFirstChild('Alien freak').Value,
		Player.UpgradeStatus:FindFirstChild('Eater').Value,
		Player.UpgradeStatus:FindFirstChild('Freaky Freddy').Value,
		Player.UpgradeStatus:FindFirstChild('Quincy freak').Value,
		Player.UpgradeStatus:FindFirstChild('Mahito freak').Value,
		Player.UpgradeStatus:FindFirstChild('Extra freaky Bob').Value,
		
		--Rebirths.Value,
		--Multiplier.Value,
		--RebirthCost.Value
	}
	
	DataStore:SetAsync(Player.UserId, ValuesToSave)
	
end)

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetChildren()) do
		plr:Kick()
	end
end)

I copied this from sum guy from youtube and now understand that this was a bad idea

Well it is never a bad idea to try and learn. so when you said

are you saying there’s already active game / already data… and you just wanted to add rebirths too?

edit: At the moment I’m just debating what to recommend and how I should proceed with assisting you not sure how much you would understand.(debating starting from scratch with better systems or fixing what you have currently)

no, its not active yet, as i said - im trying to make a first game

Do you know what module scripts are? I was thinking of just whipping together an example using module scripts, but it might be too advanced for a beginner.

edit: You also mentioned adding rebirth are you saying other data works just fine? if that happens to be the case ill just overlook what you have and help polish it

yeah, I created a topic here but nothing seems to be working. And yeah, ik what module scripts are.

by nothing, do you mean the entire script? Because I was under the assumption it was all working and you just needed rebirths to be added.

edit: I’m reviewing everything right now.

yeah, i need to add rebirths saving, other parts of the game are fine

im seeing this

like all 8 of them, are you using this inventory? each bool value. it gets the job done I suppose but is a bit tedious

no, im storing them in list of all data, maybe this isnt right, but I need that and this is working

It’s not efficient, but if it works, that’s all that matters starting out. now I’m trying to marry down on the problem with rebirths in the example you provided I’m seeing commented-out code not seeing this

this seems to be in an entirely different script