if not player:FindFirstChild('Level') then level = Instance.new('NumberValue', player) level.Name = 'Level' level.Value = 0 end onlevelupetc:connect(function() player.Character.Humanoid.MaxHealth = level.Value * 40 + 350 end)
As I had previously stored Level in Character i tried storing it in Player so it wouldn't reset on death.
When I die the first time, everything works fine with the level value carrying over to the other life. However, if I level up on that second life, it says that variable level is nil on Line 8, even though it refers to the Level NumberValue which is still there. Im not sure how this could happen, could anyone help me figure it out?
SOLVE EDIT: Just needed to add an else to it to redefine it, all good
In your snippet, you don't define level
in the event that it does exist.
You can try something like this:
local level = player:FindFirstChild("Level") -- Hope that it exists if not level then -- It didn't, so I need to make a new one level = Instance.new("IntValue", player) level.Name = "Level" level.Value = 0 end
Another option would be to use :WaitForChild
, and make another script responsible for adding all stats to the player exactly once, when they join.