i have this error when it says “Players.denysk12345678.PlayerGui.XP_Lvl.Level Up:3: attempt to index nil with ‘leaderstats’.” or here is a screenshot:
Can you paste the whole code here? I can’t help or see well if you don’t.
local Player = game.Players.LocalPlayer
local XP = Player.leaderstats.XP
local Level = Player.leaderstats.Level
local bar = script.Parent.Display.BAR
GetExpNeeded = function(CurrLevel)
local x = CurrLevel
return 2x^3 + 5x^2 - 10*x + 34
end
GetTotalLevelXP = function(Level)
local total = 0
for i = Level-1, 1, -1 do
total = total + GetExpNeeded(i)
end
return total
end
CalcExpPercent = function(CurrLevel, CurrXp)
if (CurrLevel==1) then
local ratio = CurrXp/GetExpNeeded(CurrLevel)
return ratio>1 and 1 or ratio
else
local ratio = (CurrXp-GetTotalLevelXP(CurrLevel))/GetExpNeeded(CurrLevel)
print(ratio,"=",(CurrXp-GetTotalLevelXP(CurrLevel)),":",GetExpNeeded(CurrLevel))
return ratio>1 and 1 or ratio
end
end
Level.Changed:connect(function()
bar.Parent.Lvl.Text = "Level "…Level.Value
for i = 1, 5 do
bar.Parent.BorderColor3 = Color3.new(0,1,0)
bar.Parent.Lvl.TextColor3 = Color3.new(0,1,0)
wait(0.2)
bar.Parent.BorderColor3 = Color3.new(0,0,1)
bar.Parent.Lvl.TextColor3 = Color3.new(0,0,1)
wait(0.2)
end
local xScale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+xScale, 2, 0, 2)
bar.Parent.BorderColor3 = Color3.new(1, 204/255,0)
bar.Parent.Lvl.TextColor3 = Color3.new(102/255,1,102/255)
end)
XP.Changed:connect(function()
local xScale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+xScale, 2, 0, 2)
if (xScale==1) then
Level.Value = Level.Value + 1
end
end)
local scale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+scale, 2, 0, 2)
bar.Parent.BorderColor3 = Color3.new(1, 204/255,0)
bar.Parent.Lvl.TextColor3 = Color3.new(102/255,1,102/255)
bar.Parent.Lvl.Text = "Level "…Level.Value
I’ll just format the code for you…
local Player = game.Players.LocalPlayer
local XP = Player.leaderstats.XP
local Level = Player.leaderstats.Level
local bar = script.Parent.Display.BAR
GetExpNeeded = function(CurrLevel)
local x = CurrLevel
return 2x^3 + 5x^2 - 10*x + 34
end
GetTotalLevelXP = function(Level)
local total = 0
for i = Level-1, 1, -1 do
total = total + GetExpNeeded(i)
end
return total
end
CalcExpPercent = function(CurrLevel, CurrXp)
if (CurrLevel==1) then
local ratio = CurrXp/GetExpNeeded(CurrLevel)
return ratio>1 and 1 or ratio
else
local ratio = (CurrXp-GetTotalLevelXP(CurrLevel))/GetExpNeeded(CurrLevel)
print(ratio,"=",(CurrXp-GetTotalLevelXP(CurrLevel)),":",GetExpNeeded(CurrLevel))
return ratio>1 and 1 or ratio
end
end
Level.Changed:connect(function()
bar.Parent.Lvl.Text = "Level "…Level.Value
for i = 1, 5 do
bar.Parent.BorderColor3 = Color3.new(0,1,0)
bar.Parent.Lvl.TextColor3 = Color3.new(0,1,0)
wait(0.2)
bar.Parent.BorderColor3 = Color3.new(0,0,1)
bar.Parent.Lvl.TextColor3 = Color3.new(0,0,1)
wait(0.2)
end
local xScale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+xScale, 2, 0, 2)
bar.Parent.BorderColor3 = Color3.new(1, 204/255,0)
bar.Parent.Lvl.TextColor3 = Color3.new(102/255,1,102/255)
end)
XP.Changed:connect(function()
local xScale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+xScale, 2, 0, 2)
if (xScale==1) then
Level.Value = Level.Value + 1
end
end)
local scale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+scale, 2, 0, 2)
bar.Parent.BorderColor3 = Color3.new(1, 204/255,0)
bar.Parent.Lvl.TextColor3 = Color3.new(102/255,1,102/255)
bar.Parent.Lvl.Text = "Level "…Level.Value
Anyways, which line did it error?
local Player = game.Players.LocalPlayer
local XP = Player.leaderstats.XP
local Level = Player.leaderstats.Level
LocalPlayer
only returns a player in localscripts, you’re using it in a regular script which returns nil
it says: Players.denysk12345678.PlayerGui.DevButton.TextButton.Script:2: attempt to index nil with ‘WaitForChild’ - Server - Script:2
Well you see, LocalPlayer
is only for local scripts, NOT server scripts.
local Player = game.Players.LocalPlayer
local XP = Player.leaderstats.XP
local Level = Player.leaderstats.Level
local bar = script.Parent.Display.BAR
GetExpNeeded = function(CurrLevel)
local x = CurrLevel
return 2x^3 + 5x^2 - 10*x + 34
end
GetTotalLevelXP = function(Level)
local total = 0
for i = Level-1, 1, -1 do
total = total + GetExpNeeded(i)
end
return total
end
CalcExpPercent = function(CurrLevel, CurrXp)
if (CurrLevel==1) then
local ratio = CurrXp/GetExpNeeded(CurrLevel)
return ratio>1 and 1 or ratio
else
local ratio = (CurrXp-GetTotalLevelXP(CurrLevel))/GetExpNeeded(CurrLevel)
print(ratio,"=",(CurrXp-GetTotalLevelXP(CurrLevel)),":",GetExpNeeded(CurrLevel))
return ratio>1 and 1 or ratio
end
end
Level.Changed:connect(function()
bar.Parent.Lvl.Text = "Level "…Level.Value
for i = 1, 5 do
bar.Parent.BorderColor3 = Color3.new(0,1,0)
bar.Parent.Lvl.TextColor3 = Color3.new(0,1,0)
wait(0.2)
bar.Parent.BorderColor3 = Color3.new(0,0,1)
bar.Parent.Lvl.TextColor3 = Color3.new(0,0,1)
wait(0.2)
end
local xScale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+xScale, 2, 0, 2)
bar.Parent.BorderColor3 = Color3.new(1, 204/255,0)
bar.Parent.Lvl.TextColor3 = Color3.new(102/255,1,102/255)
end)
XP.Changed:connect(function()
local xScale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+xScale, 2, 0, 2)
if (xScale==1) then
Level.Value = Level.Value + 1
end
end)
local scale = CalcExpPercent(Level.Value, XP.Value)
bar.Position = UDim2.new(-1+scale, 2, 0, 2)
bar.Parent.BorderColor3 = Color3.new(1, 204/255,0)
bar.Parent.Lvl.TextColor3 = Color3.new(102/255,1,102/255)
bar.Parent.Lvl.Text = "Level "…Level.Value
I fixed the indentation to help readability
ok i fixed it but i gave me another error: Players.denysk12345678.PlayerGui.DevButton.TextButton.LocalScript:5: attempt to perform arithmetic (add) on Instance and number
script:
local stat = game.Players.LocalPlayer:WaitForChild(“leaderstats”)
script.Parent.MouseButton1Click:Connect(function()
stat.Gold = stat.Gold + 10
stat.Level = stat.Level + 1
end)
This means, you are trying to perform mathematics calculation using an Instance with a number, you are referring the Value instances, so you should add a .Value
after each Value instance you specificed.