This script works in the sense that it makes all the right instances, but when I change the value of EXP to, say, 100, my player doesn't level up to level 2.
game.Players.PlayerAdded:connect(function(plr)
local stats = Instance.new('IntValue', plr)
stats.Name = 'leaderstats'
experience = Instance.new('IntValue', stats)
experience.Name = 'EXP'
experience.Value = 0
level = Instance.new('IntValue',stats)
level.Name = 'Level'
level.Value = 0
end)
function level()
local required=(level.Value*100)
if experience.Value>=required then
level.Value=level.Value+1
experience.Value=experience.Value-required
level()
wait(.01)
end
end
experience.Changed:connect(level)
I get the error Workspace.Script:22: attempt to index global 'experience' (a nil value) This means that somewhere on line 22 I went wrong. I don't see how, because experience is defined correctly and the fuction's name is level. Any help is appreciated, thank you for reading.

1 comment
This works but there was just a small problem. I had to move the function call after the function so that it checks if the player leveled up after the experience is gained, as suggested by another user on this site