Datastore not working

I am having a problem with saving data in a datastore. I have been working on this same thing for an excessively long time without success. I am hoping you can find what I am doing wrong.
script:

local DDS = game:GetService("DataStoreService"):GetDataStore("DataThingy")

game.Players.PlayerAdded:Connect(function(Plr)
	
	
	local stats = Instance.new("Folder", Plr)
	stats.Name = "leaderstats"
	
	local levels = Instance.new("IntValue", stats)
	levels.Name = "Levels"
	
	local exp = Instance.new("IntValue", stats)
	exp.Name = "Exp"
	
	local data 
	local suc, err = pcall(function()
		data = DDS:GetAsync("key_"..Plr.UserId)
	end)
	if not suc then
		warn(err)
	else
		levels.Value = data[Level]
		exp.Value = data[Experience]
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local leaderstatsInTable = {
		Level = plr.leaderstats.Levels.Value,
		Experience = plr.leaderstats.Exp.Value
	}
	
	local data 
	local suc, err = pcall(function()
		local data = DDS:SetAsync("key_"..plr.UserId, leaderstatsInTable)
	end)
	if not suc then
		warn(err)
	end
end)

Error?

error:

[ServerScriptService.Put in SSS:22: attempt to index number with nil]

Please paste line 22.

This is line 22:

levels.Value = data[Level]

You said to paste line 22, so I did.

Change your code to this, see if it does something:

local DDS = game:GetService("DataStoreService"):GetDataStore("DataThingy")

game.Players.PlayerAdded:Connect(function(Plr)
	
	
	local stats = Instance.new("Folder", Plr)
	stats.Name = "leaderstats"
	
	local levels = Instance.new("IntValue", stats)
	levels.Name = "Levels"
	
	local exp = Instance.new("IntValue", stats)
	exp.Name = "Exp"
	
	local data 
	local suc, err = pcall(function()
		data = DDS:GetAsync("key_"..Plr.UserId)
	end)
	if not suc then
		warn(err)
	else
		levels.Value = data['Level']
		exp.Value = data['Experience']
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local leaderstatsInTable = {
		Level = plr.leaderstats.Levels.Value,
		Experience = plr.leaderstats.Exp.Value
	}
	
	local data 
	local suc, err = pcall(function()
		data = DDS:SetAsync("key_"..plr.UserId, leaderstatsInTable)
	end)
	if not suc then
		warn(err)
	end
end)

Edit: Changed the code a bit try now @blockydiamond01

I’m pretty sure at line 20 and 21, you should be getting the value from the table with a string.

levels.Value = data['Level']
exp.Value = data['Experience']

I tried it and it still didn’t work.

Any new error?

It still has the same error sadly.

You used my updated code, right? I edited the post.

Yes, I did. It still didn’t work.

Try doing this. I don’t know if it will work, though. Now I know how to put it in a code box.

local DDS = game:GetService("DataStoreService"):GetDataStore("DataThingy")

game.Players.PlayerAdded:Connect(function(Plr)
	
	
	local stats = Instance.new("Folder", Plr)
	stats.Name = "leaderstats"
	
	local levels = Instance.new("IntValue", stats)
	levels.Name = "Levels"
	
	local exp = Instance.new("IntValue", stats)
	exp.Name = "Exp"
	
	local data 
	local suc, err = pcall(function()
		data = DDS:GetAsync("key_"..Plr.UserId)
	end)
	if not suc then
		warn(err)
	else
		levels.Value = data['Levels']
		exp.Value = data['Exp']
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local leaderstatsInTable = {
		Level = plr.leaderstats.Levels.Value,
		Experience = plr.leaderstats.Exp.Value
	}
	
	local data 
	local suc, err = pcall(function()
		data = DDS:SetAsync("key_"..plr.UserId, leaderstatsInTable)
	end)
	if not suc then
		warn(err)
	end
end)

Show me your updated line 22

also please reply to my posts, instead of to the topic

levels.Value = data[‘Levels’]
exp.Value = data[‘Exp’]

Uhh ok

These are lines 22 and 23(I think 23 is also an error).

levels.Value = data[‘Level’]
exp.Value = data[‘Experience’]

levels.Value = data[‘Level’] or 0
exp.Value = data[‘Experience’] or 0

try changing your line 22 & 23 to this

It is still an error. or 0 isn’t needed since the data is set to 0 by default (this is just my reasoning).