Confusing data save glitch

Hello,

I have a data save script, but there’s an issue with it in-game. This error message shows up when you join the game:

This has never happened before, and I don’t know what causes it or how to fix it

Here’s the script:

local DS = game:GetService("DataStoreService"):GetDataStore("Anorbs")

game.Players.PlayerAdded:Connect(function(p)
	local folder = Instance.new("Folder",p)
	folder.Name = "animalOrbs"
	
	local spe = 	Instance.new("BoolValue",folder)
	spe.Name = "speed orb"
	
	local hp = Instance.new("BoolValue",folder)
	hp.Name = "health orb"
	
	local j = Instance.new("BoolValue",folder)
	j.Name = "jump orb"
	
	local crit = Instance.new("BoolValue",folder)
	crit.Name = "crit orb"
	
	local sm = Instance.new("BoolValue",folder)
	sm.Name = "??? orb"
	
	local orb = Instance.new("StringValue",folder)
	orb.Name = "animalEquipped"
	
	local name = Instance.new("StringValue",folder)
	name.Name = "animalName"

	wait(0.2)
	local plrKey = "orb_"..p.UserId

	local GetSaved = DS:GetAsync(plrKey)
	if GetSaved then
		spe.Value = GetSaved[1]
		hp.Value = GetSaved[2]
		j.Value = GetSaved[3]
		crit.Value = GetSaved[4]
		orb.Value = GetSaved[5]  --- this line is what causes the error. I don't know why
		name.Value = GetSaved[6]
		sm.Value = GetSaved[7]
	else 
		local NumbersForSaving = {
			spe.Value,
			hp.Value,
			j.Value,
			crit.Value,
			orb.Value,
			name.Value,
			sm.Value,
		}
		DS:GetAsync(plrKey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	DS:SetAsync("orb_"..p.UserId, {
		p.animalOrbs["speed orb"].Value,
		p.animalOrbs["health orb"].Value,
		p.animalOrbs["jump orb"].Value,
		p.animalOrbs["crit orb"].Value,
		p.animalOrbs["??? orb"].Value,
		p.animalOrbs.animalEquipped.Value,
		p.animalOrbs.animalName.Value,
	})
end)

Looks like because you confused the variables a bit, your ordering shoudl be

spe.Value = GetSaved[1]
hp.Value = GetSaved[2]
j.Value = GetSaved[3]
crit.Value = GetSaved[4]
sm.Value = GetSaved[5] 
orb.Value = GetSaved[6]
name.Value = GetSaved[7]

sm is the variable that holds the ??? Orb boolvalue, orb contains AnimalEquipped, which is a StringValue

you are not retrieving them in the correct order orb.Value is at index 6 you put it as 5

the same error appears when I rearrange the order

Didn’t notice this in your else statement, I think yo meant to do SetAsync here? Although I think in your else you just set default values if data was not found instead of making a table and setting it into a datastore. It’s unrelated however but seems like a small oversight,

Back on topic,

Do a print(GetSaved) after the if GetSaved then to see how the order is being given if Data was found. Likely something is wrong with the ordering agin

oddly enough, adding a line that says print(GetSaved[5]) before the line made it work somehow

Huh? That shouldn’t have changed anything? It’s likely it could’ve been a 1 time error, maybe the script didn’t update when you tested it so it still gave that error until it got updated? Try removing it and see if the error comes back?

Copy this to console and execute it:

local success, ErrorMessage = pcall(function()
	game:GetService("DataStoreService"):GetDataStore("Anorbs"):RemoveAsync(112659944)
end)
if not success then warn(ErrorMessage) end

Then, try use this script:

local DS = game:GetService("DataStoreService"):GetDataStore("Anorbs")

game:GetService("Players").PlayerAdded:Connect(function(p)
	local folder = Instance.new("Folder",p)
	folder.Name = "animalOrbs"
	
	local spe = Instance.new("BoolValue",folder)
	spe.Name = "speed orb"
	
	local hp = Instance.new("BoolValue",folder)
	hp.Name = "health orb"
	
	local j = Instance.new("BoolValue",folder)
	j.Name = "jump orb"
	
	local crit = Instance.new("BoolValue",folder)
	crit.Name = "crit orb"
	
	local sm = Instance.new("BoolValue",folder)
	sm.Name = "??? orb"
	
	local orb = Instance.new("StringValue",folder)
	orb.Name = "animalEquipped"
	
	local name = Instance.new("StringValue",folder)
	name.Name = "animalName"

	wait(0.2)
	local plrKey = "orb_"..p.UserId

	local success, GetSaved = pcall(function()
		return DS:GetAsync(plrKey)
	end)
	
	if success and GetSaved then
		spe.Value = GetSaved[1]
		hp.Value = GetSaved[2]
		j.Value = GetSaved[3]
		crit.Value = GetSaved[4]
		sm.Value = GetSaved[5] 
		orb.Value = GetSaved[6]
		name.Value = GetSaved[7]
	elseif success then
		local NumbersForSaving = {
			spe.Value,
			hp.Value,
			j.Value,
			crit.Value,
			sm.Value,
			orb.Value,
			name.Value,
		}
		local success, errorMessage = pcall(function()
			DS:SetAsync(plrKey, NumbersForSaving)
		end)

		if not success then warn(errorMessage) end
	end
end)

local queue = 0

game:GetService("Players").PlayerRemoving:Connect(function(p)
	queue += 1
	local success, errorMessage = pcall(function()
		DS:SetAsync("orb_"..p.UserId, {
			p.animalOrbs["speed orb"].Value,
			p.animalOrbs["health orb"].Value,
			p.animalOrbs["jump orb"].Value,
			p.animalOrbs["crit orb"].Value,
			p.animalOrbs["??? orb"].Value,
			p.animalOrbs.animalEquipped.Value,
			p.animalOrbs.animalName.Value,
		})
	end)

	queue -= 1
	if not success then warn(errorMessage) end
end)

game:BindToClose(function()
	repeat wait() until queue == 0
end)

I just edited it

You should probably have your DataSaves in a separate script, not with the script that creates the values.