Level script resulting in an end error?

I have a level up script which results in an error at the end I dont know why since I keep removing, adding brackets and adding on more ends but it doesn’t seem to work at all?

This is the script :

while task.wait() do
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV
		
	if LV <=10 then
		if     EXP.Value >=LV.Value  * LV.Value  * 100 + 400 then
			LV.Value  = ((EXP.Value -400 )/100)^.5
			end
		end
	end)
	
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV

	if LV.Value  >10 and LV.Value  <=20 then
		if     EXP.Value  >=LV.Value  * LV.Value  * 150 + 400 then
			LV.Value  = ((EXP.Value -400 )/150)^.5
			end
		end
	end)
	
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV
		
	if LV.Value  >20 and LV.Value  <30  then
		if     EXP.Value  >=LV.Value  * LV.Value  * 200 + 400 then
			LV.Value  = ((EXP.Value -400 )/200)^.5
			end
		end
	end)
	
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV
		
	if LV.Value  >= 30 and LV.Value <= 50  then
		if     EXP.Value  >= (LV.Value^3 * 10) + 400 then
			LV.Value  = LV.Value  + 1
			end
		end
	end)
	
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV
		
	if LV.Value  > 50 and EXP.Parent.BeatD7.Value == 1 then
		if     EXP.Value  >= LV.Value^3.6 then
				LV.Value = EXP.Value^(1/3.6)
				end
			end
		end
	end
end

I dont know why it doesnt work its only the last line I’ve tried multiple different iterations of ending but still doesn’t seem to work help would be appreciated thank you!

Can you please screenshot and send the exact error message?

Also, try this version of the script:

while task.wait() do
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV

		if LV <=10 then
			if     EXP.Value >=LV.Value  * LV.Value  * 100 + 400 then
				LV.Value  = ((EXP.Value -400 )/100)^.5
			end
		end
	end)

	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV

		if LV.Value  >10 and LV.Value  <=20 then
			if     EXP.Value  >=LV.Value  * LV.Value  * 150 + 400 then
				LV.Value  = ((EXP.Value -400 )/150)^.5
			end
		end
	end)

	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV

		if LV.Value  >20 and LV.Value  <30  then
			if     EXP.Value  >=LV.Value  * LV.Value  * 200 + 400 then
				LV.Value  = ((EXP.Value -400 )/200)^.5
			end
		end
	end)

	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV

		if LV.Value  >= 30 and LV.Value <= 50  then
			if     EXP.Value  >= (LV.Value^3 * 10) + 400 then
				LV.Value  = LV.Value  + 1
			end
		end
	end)

	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)
		local leaderstats = player:FindFirstChild("leaderstats")
		local EXP = leaderstats.EXP
		local LV = leaderstats.LV

		if LV.Value  > 50 and EXP.Parent.BeatD7.Value == 1 then
			if     EXP.Value  >= LV.Value^3.6 then
				LV.Value = EXP.Value^(1/3.6)
			end
		end
	end)
end

I chucked it into studio, formatted it, and corrected some of the syntax issues

1 Like

Oh it works thank you! What seems to be the problem since I had absolutely no clue?

You had one too many ends, and were missing a closing bracket ) on one of the ends

Oh alright, But now I’ve resulted in a nil error.
ServerScriptService.Level:5: attempt to index nil with ‘EXP’
image

image
image
Not sure why I usually get this error alot

try making it player:WaitForChild("leaderstats") instead, it might help.

edit:
Looking at your script, it is REALLY unoptimised lol. It’ll probably crash. gimme a moment to improve it

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats", 10)
	local EXP = leaderstats.EXP
	local LV = leaderstats.LV

	if LV <=10 then
		if EXP.Value >=LV.Value  * LV.Value  * 100 + 400 then
			LV.Value  = ((EXP.Value -400 )/100)^.5
		end
	elseif LV.Value  > 10 and LV.Value  <=20 then
		if EXP.Value  >= LV.Value  * LV.Value  * 150 + 400 then
			LV.Value  = ((EXP.Value -400 )/150)^.5
		end
	elseif LV.Value  > 20 and LV.Value  <30  then
		if EXP.Value  >=LV.Value  * LV.Value  * 200 + 400 then
			LV.Value  = ((EXP.Value -400 )/200)^.5
		end
	elseif LV.Value  >= 30 and LV.Value <= 50  then
		if     EXP.Value  >= (LV.Value^3 * 10) + 400 then
			LV.Value  = LV.Value  + 1
		end
	elseif LV.Value  > 50 and EXP.Parent.BeatD7.Value == 1 then
		if     EXP.Value  >= LV.Value^3.6 then
			LV.Value = EXP.Value^(1/3.6)
		end
	end 
end)

Oh thanks what’s the 10 for after leaders tats though? And I’ll try out that then thank you

Gives it a maximum of 10 seconds to find leaderstats before it gives up.

Can you please send the code you have that creates leaderstats?

The error you are getting basically means that the object EXP does not exist and you are indexing nothing; try this:

local leaderstats = player:WaitForChild('leaderstats')
local EXP = leaderstats:WaitForChild('EXP')
local LV = leaderstats:WaitForChild('LV')

The error means that leaderstats doesn’t exist, (Attempt to index nil with…). Which is why I asked for the code that create’s their leaderstats

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("LeaderStats")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local EXP = Instance.new("IntValue", Leaderstats)
	EXP.Name = "EXP" 
	EXP.Value = 0
	local LV = Instance.new("IntValue", Leaderstats)
	LV.Name = "LV" 
	LV.Value = 1
	local G = Instance.new("IntValue", Leaderstats)
	G.Name = "G" 
	G.Value = 0
end)

And oh alright thank you!

Did putting the :WaitForChild() thing help at all?

It did but now I’m getting the error

: attempt to compare Instance <= number

nevermind fixed it since it wasnt checking for the leaderstat’s value but now it just doesn’t level me up at all…?
https://gyazo.com/721fb244111b5ac816c8976b954aeccb

Attempt to index nil with EXP means that you are trying to index the EXP instance which doesnt exist (nil); adding the :WaitForChild() ensures that the EXP instance loads in the folder before returning it.

And as @Kurtziru said adding the :WaitForChild() did fix this error

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local EXP = leaderstats.EXP.Value
local LV = leaderstats.LV.Value
while task.wait() do
	if LV <=10 then
		if EXP >=LV  * LV  * 100 + 400 then
			LV  = ((EXP -400 )/100)^.5
		end
	elseif LV  > 10 and LV  <=20 then
		if EXP  >= LV  * LV  * 150 + 400 then
			LV  = ((EXP -400 )/150)^.5
		end
	elseif LV  > 20 and LV  <30  then
		if EXP  >=LV  * LV  * 200 + 400 then
			LV  = ((EXP -400 )/200)^.5
		end
	elseif LV  >= 30 and LV <= 50  then
		if     EXP  >= (LV^3 * 10) + 400 then
			LV  = LV  + 1
		end
	elseif LV  > 50 and LV <= 50 then
		if     EXP  >= LV^3.6 then
			LV = EXP^(1/3.6)
			end
		end 
	end
end)

I put a while wait do and it still doesn’t do anything? Do you know why possibly

Attempt to index nil with EXP

Break down the message:

Attempt to index - attempt to get a value from
nil - non existant value
with Exp - get the value of Exp

Which results as:
Attempt to get the value of Exp from a non existant value.

Therefore: leaderstats has no value, not Exp. As I have been saying…

While loops are probably not the best solution here, but if you do wish to use them:
Put

local EXP = leaderstats.EXP.Value
local LV = leaderstats.LV.Value

INSIDE the loop:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local EXP = leaderstats.EXP.Value
local LV = leaderstats.LV.Value
while task.wait() do
	if LV <=10 then
		if EXP >=LV  * LV  * 100 + 400 then
			LV  = ((EXP -400 )/100)^.5
		end
	elseif LV  > 10 and LV  <=20 then
		if EXP  >= LV  * LV  * 150 + 400 then
			LV  = ((EXP -400 )/150)^.5
		end
	elseif LV  > 20 and LV  <30  then
		if EXP  >=LV  * LV  * 200 + 400 then
			LV  = ((EXP -400 )/200)^.5
		end
	elseif LV  >= 30 and LV <= 50  then
		if     EXP  >= (LV^3 * 10) + 400 then
			LV  = LV  + 1
		end
	elseif LV  > 50 and LV <= 50 then
		if     EXP  >= LV^3.6 then
			LV = EXP^(1/3.6)
			end
		end 
	end
end)

Updated code:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local EXP = leaderstats.EXP.Value
local LV = leaderstats.LV.Value
leaderstats.EXP:GetPropertyChangedSignal("Value"):Connect(function()
	if LV <=10 then
		if EXP >=LV  * LV  * 100 + 400 then
			LV  = ((EXP -400 )/100)^.5
		end
	elseif LV  > 10 and LV  <=20 then
		if EXP  >= LV  * LV  * 150 + 400 then
			LV  = ((EXP -400 )/150)^.5
		end
	elseif LV  > 20 and LV  <30  then
		if EXP  >=LV  * LV  * 200 + 400 then
			LV  = ((EXP -400 )/200)^.5
		end
	elseif LV  >= 30 and LV <= 50  then
		if     EXP  >= (LV^3 * 10) + 400 then
			LV  = LV  + 1
		end
	elseif LV  > 50 and LV <= 50 then
		if     EXP  >= LV^3.6 then
			LV = EXP^(1/3.6)
			end
		end 
	end
end)