Attempt to index number with 'Exp' (datastore leveling system)

So I have been working on a leveling system that HAD seemed to work perfectly fine so far into the making. However, without changing anything to do with this part of the code, it started erroring with “attempt to index number with ‘Exp’”. I have also noticed that for some strange reason it would work out of no where upon joining, then start erroring with same error again the next time. Heres the main portion of the script i need assistance on:

game.Players.PlayerAdded:connect(function(player)
	wait(5) --i tried using this to wait for the player to load but still happens when loaded.
	local ExpRequirement = player.Character.ExpRequirement
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Exp = Instance.new("NumberValue",leader)
	local Level = Instance.new("NumberValue",leader)
	Exp.Name = "Exp"
	Level.Name = "Level"
	local playerData = ds:GetAsync(player.UserId)
	if playerData then
		Exp.Value = playerData.Exp or 0 --THE ERROR
		Level.Value = playerData.Level or 1 --LIKELY TO ALSO ERROR
		player.Character.ExpRequirement.Value = playerData.RequiredExp or 100
	else
		Exp.Value = 0
		Level.Value = 1
		player.Character.ExpRequirement.Value = 100
	end
	local Data = {["Exp"] = Exp.Value, ["Level"] = Level.Value, ["RequiredExp"] = player.Character.ExpRequirement.Value}
	print(Data.Exp)
	print(Data.Level)
	print(Data.RequiredExp)
	ds:SetAsync(player.UserId, Data.Exp)
	ds:SetAsync(player.UserId, Data.Level)
	UpdateLevelStats(player, Exp.Value, Level.Value, player.Character.EarnedExp)
	Exp.Changed:connect(function()
		ds:SetAsync(player.UserId, Data.Exp)
		UpdateLevelStats(player, Exp.Value, Level.Value, player.Character.EarnedExp)
	end)
	Level.Changed:Connect(function()
		ds:SetAsync(player.UserId, Data.Level)
	end)
	player.Character.EarnedExp.Changed:Connect(function()
		ds:SetAsync(player.UserId, Data.RequiredExp)
	end)
end)

if for whatever reason you need to see my whole script, heres the full thing:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
local Multiplier = 1.1

local function UpdateLevelStats(player, Exp, Level, EarnedExp)
	warn("check stats")
	local expRequirement = player.Character.ExpRequirement
	local SizeGain = 0.2
	
	expRequirement.Value -= EarnedExp.Value
	
	while Level <= 200 do
		wait()
		if expRequirement.Value <= 0 then
			warn("updating stats")
			player.leaderstats.Level.Value += 1
			expRequirement.Value = 100
			Multiplier = Multiplier * 2
			player.Character:ScaleTo(player.Character:GetScale() + 0.2)
			if Level == 150 then --WHEN LEVEL 150 REACHED
				SizeGain += 0.2
				player.Character.Stats.MovementSpeed.Value += 0.3
			else
				player.Character.Stats.MovementSpeed.Value += 0.1
			end
			
			if Level % 50 == 0 then --EVERY 50 LEVELS
				player.Character.Stats.MaxHealth.Value += 3
				player.Character.Humanoid.Health = player.Character.Stats.MaxHealth.Value
				player.Character.Stats.Dmg.Value += 0.5
				SizeGain += 0.1
				expRequirement.Value = expRequirement.Value * 1.08  -- 110% requirement every 50 levels
				expRequirement.Value -= 0.02    -- 2% drop in scale increase every 50 levels
			else --EVERY 1 LEVEL ((OTHER THAN EVERY 50))
				player.Character.Stats.MaxHealth.Value += 1
				player.Character.Humanoid.Health = player.Character.Stats.MaxHealth.Value
				player.Character.Stats.Dmg.Value += 0.2
				expRequirement.Value = expRequirement.Value * Multiplier   -- 110% requirement for regular levels
			end
		else
			break
		end
	end
end
game.Players.PlayerAdded:connect(function(player)
	wait(5) --i tried using this to wait for the player to load but still happens when loaded.
	local ExpRequirement = player.Character.ExpRequirement
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Exp = Instance.new("NumberValue",leader)
	local Level = Instance.new("NumberValue",leader)
	Exp.Name = "Exp"
	Level.Name = "Level"
	local playerData = ds:GetAsync(player.UserId)
	if playerData then
		Exp.Value = playerData.Exp or 0 --THE ERROR
		Level.Value = playerData.Level or 1 --LIKELY TO ALSO ERROR
		player.Character.ExpRequirement.Value = playerData.RequiredExp or 100
	else
		Exp.Value = 0
		Level.Value = 1
		player.Character.ExpRequirement.Value = 100
	end
	local Data = {["Exp"] = Exp.Value, ["Level"] = Level.Value, ["RequiredExp"] = player.Character.ExpRequirement.Value}
	print(Data.Exp)
	print(Data.Level)
	print(Data.RequiredExp)
	ds:SetAsync(player.UserId, Data.Exp)
	ds:SetAsync(player.UserId, Data.Level)
	UpdateLevelStats(player, Exp.Value, Level.Value, player.Character.EarnedExp)
	Exp.Changed:connect(function()
		ds:SetAsync(player.UserId, Data.Exp)
		UpdateLevelStats(player, Exp.Value, Level.Value, player.Character.EarnedExp)
	end)
	Level.Changed:Connect(function()
		ds:SetAsync(player.UserId, Data.Level)
	end)
	player.Character.EarnedExp.Changed:Connect(function()
		ds:SetAsync(player.UserId, Data.RequiredExp)
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	local NewData = {["Exp"] = player.leaderstats.Exp.Value, ["Level"] = player.leaderstats.Level.Value, ['RequiredExp'] = player.Character.ExpRequirement.Value}
	print(NewData.Exp)
	print(NewData.Level)
	print(NewData.RequiredExp)
	ds:SetAsync(player.UserId, NewData)
end)

when the script was working, everything was working as attended.

This is the problem, you are setting the data to a number (-> error). You shouldn’t do this every time the data changes because it will exceed DataStore limits. Just save it when the player leaves

this could explain data randomly resetting as well. ty for the advice i will attempt this

ive determined this is not the issue. I have tested this and still no go. i have also noticed there are no datastore errors such as timeout or forbidden or even in queue

i overlooked this and realized what u meant, all works now. ty

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.