Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to index nil with number when adding another data value?

So I made a dataStore script and its worked perfectly until I added a new variable "SpinVisibility"

im not sure whats exactly wrong with it but I keep getting the error

"line 99 attempt to index nil with number"

now I got this last error for my HasGame variable, and I found the solution to that was that I forgot to set it in both the table

this part

	local tableToSave = {
		player.Data.HasGame.Value, -- First value from the table
		player.Data.Hair.Value,
		player.Data.Outfit.Value,
		player.Data.Accessory.Value,
		player.Data.Gender.Value,
		player.Data.Blood.Value,
		player.Data.Spins.Value,
		player.Data.SpinVisibility.Value
	}

and this part


	if success then

		HasGame.Value = data[1]
		Hair.Value = data[2]
		Outfit.Value = data[3]
		Accessory.Value = data[4]
		Gender.Value = data[5]
		Blood.Value = data[6]
		Spins.Value = data[7]
		SpinVisibility.Value = data[8]

so tldr: I only made 6 values in the first one but I forgot about HasGame and in the 2nd part there were 7 values and thats how I got the error "attempt to index nil with number" and I also changed "player" to "plr" as its a script in serverscriptservice

When I fixed those parts my Spin Gui worked perfectly, but I needed a new variable for the visibility of the spin button so the player doesnt abuse it when they rejoin

and thats how it all led here, sorry if it seems like a life story but ever since ive added the SpinVisibility variable its given me the same error on line 99

HasGame.Value = data[1]

that part

ive checked for everything wrong with it and tried to fix it like i fixed the hasgame variable but nothings worked, tell me whats wrong with it please

heres the full datastore script

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Ballin2")
local replicatedStorage = game:GetService("ReplicatedStorage")
local saveRemote = replicatedStorage:WaitForChild("Save")



local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.Data.HasGame.Value, -- First value from the table
		player.Data.Hair.Value,
		player.Data.Outfit.Value,
		player.Data.Accessory.Value,
		player.Data.Gender.Value,
		player.Data.Blood.Value,
		player.Data.Spins.Value,
		player.Data.SpinVisibility.Value
	}
	

	local success, err = pcall(function()
		DataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves the game
	saveData(player) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do
		saveData(player) -- Save the data
	end
end)

--creating data

game.Players.PlayerAdded:Connect(function(plr)
	
	wait()
	
	local folder = Instance.new("Folder", plr)
	folder.Name = "Data"
	
	
	local HasGame = Instance.new("BoolValue", folder)
	HasGame.Name = "HasGame"
	
	local Hair = Instance.new("StringValue", folder)
	Hair.Name = "Hair"

	local Outfit = Instance.new("StringValue", folder)
	Outfit.Name = "Outfit"

	local Accessory = Instance.new("StringValue", folder)
	Accessory.Name = "Accessory"
	
	local Gender = Instance.new("StringValue", folder)
	Gender.Name = "Gender"
	
	local Blood = Instance.new("StringValue", folder)
	Blood.Name = "Blood"
	
	local Spins = Instance.new("IntValue", folder)
	Spins.Name = "Spins"
	
	local SpinVisibility = Instance.new("BoolValue", folder)
	SpinVisibility.Name = "SpinVisibility"
	
	
	
    --visibiliy of spin button variable
	

	

	------------------------------------------------------------------------------------------------------------------------SAME HERE
	
	

	local data
	local success, err = pcall(function()

		data = DataStore:GetAsync(plr.UserId)

	end)

	if success then

		HasGame.Value = data[1]
		Hair.Value = data[2]
		Outfit.Value = data[3]
		Accessory.Value = data[4]
		Gender.Value = data[5]
		Blood.Value = data[6]
		Spins.Value = data[7]
		SpinVisibility.Value = data[8]

	else
		print("The player has no data!") -- The default will be set to 0
		HasGame.Value = false
		Hair.Value = "ConfirmedNewSave"
		Outfit.Value = "ConfirmedOutfitNewSave"
		Accessory.Value = "ConfirmedHornsNewSave"
		Gender.Value = "Male"
		Blood.Value = "Burgundy"
		Spins.Value = 8
		SpinVisibility.Value = true
	end

end)

2 comments

0

You probably need to reset the DataStore. What I assume is happening is that you're trying to retrieve a DataStore value that does not exist. The DataStore isn't empty, that's why the pcall() isn't failing. But the DataStore has nil values in the table.

0

i mean I change the datastores name "Ballin" to "Ballin2" im not sure if thats what you mean tho

1 answer

for those of you at home

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Ballin")
local replicatedStorage = game:GetService("ReplicatedStorage")
local saveRemote = replicatedStorage:WaitForChild("Save")



local function saveData(plr) -- The functions that saves data

	local tableToSave = {
		plr.Data.HasGame.Value, -- First value from the table
		plr.Data.Hair.Value,
		plr.Data.Outfit.Value,
		plr.Data.Accessory.Value,
		plr.Data.Gender.Value,
		plr.Data.Blood.Value,
		plr.Data.Spins.Value,
		plr.Data.SpinVis.Value
	}
	

	local success, err = pcall(function()
		DataStore:SetAsync(plr.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)
	
	
	if success then
		print("Data has been saved!")
	else
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerRemoving:Connect(function(plr) -- When a player leaves the game
	saveData(plr) -- Save the data
end)

game:BindToClose(function() -- When the server shuts down
	for _, plr in pairs(game.Players:GetPlayers()) do
		saveData(plr) -- Save the data
	end
end)

--creating data

game.Players.PlayerAdded:Connect(function(plr)
	
	wait()
	
	local folder = Instance.new("Folder", plr)
	folder.Name = "Data"
	
	
	local HasGame = Instance.new("BoolValue", folder)
	HasGame.Name = "HasGame"
	
	local Hair = Instance.new("StringValue", folder)
	Hair.Name = "Hair"

	local Outfit = Instance.new("StringValue", folder)
	Outfit.Name = "Outfit"

	local Accessory = Instance.new("StringValue", folder)
	Accessory.Name = "Accessory"
	
	local Gender = Instance.new("StringValue", folder)
	Gender.Name = "Gender"
	
	local Blood = Instance.new("StringValue", folder)
	Blood.Name = "Blood"
	
	local Spins = Instance.new("IntValue", folder)
	Spins.Name = "Spins"
	
	local SpinVis = Instance.new("BoolValue", folder)
	SpinVis.Name = "SpinVis"
	
	
	
    --visibiliy of spin button variable
	

	

	------------------------------------------------------------------------------------------------------------------------SAME HERE
	
	

	local data
	local success, err = pcall(function()

		data = DataStore:GetAsync(plr.UserId)

	end)

	if success then

		HasGame.Value = data[1]
		Hair.Value = data[2]
		Outfit.Value = data[3]
		Accessory.Value = data[4]
		Gender.Value = data[5]
		Blood.Value = data[6]
		Spins.Value = data[7]
		SpinVis.Value = data[8]

	else
		print("The player has no data!") -- The default will be set to 0
		HasGame.Value = false
		Hair.Value = "ConfirmedNewSave"
		Outfit.Value = "ConfirmedOutfitNewSave"
		Accessory.Value = "ConfirmedHornsNewSave"
		Gender.Value = "Male"
		Blood.Value = "Burgundy"
		Spins.Value = 8
		SpinVis.Value = true
	end

end)

and im still getting the error on specifically HasGame.Value = data[1]

even though it SUDDENLY STOPPED WORKING when i added SpinVis god damnit its taken like 3 days for this question to actually be seen

This discussion is preserved as it was. New answers, comments, and votes are closed.