Leaderstats Problem

Hello , I was trying to make a level system with xp and more , so my problem is that I made it so if you have 100 xp your xp value changes with to 0 and the level value changes to 2 but it doesn’t work and it doesn’t print “Yes” , no errors.

code:

local st = game.Players.LocalPlayer:WaitForChild("exp")
local player = game.Players.LocalPlayer
	if st.Value >= player.expneeded.Value then
	player.exp.Value = 0
	player.lvl.Value = 2
	player.expneeded.Value = player.expneeded.Value * player.lvl.Value
print("yes")
end
1 Like

try this:

local st = game.Players.LocalPlayer:WaitForChild("exp")
local player = game.Players.LocalPlayer
   local function expFunc()
	if st.Value >= player.expneeded.Value then
    player.exp.Value -= player.expneeded.Value
	player.lvl.Value += 1
	player.expneeded.Value = player.expneeded.Value * player.lvl.Value
   if st.Value >= player.expneeded.Value then
   expFunc()
   end
   end
expFunc()
print("yes")
end
1 Like

still doesn’t work , idk why tho , I tried .Changed but nothing ;-;

1 Like

Why are you doing this in a LocalScript, and do you have a server script that creates the “expneeded” & “exp” values?

2 Likes

You are supposed to be handling EXP and Levels on the server, because if you do it on the client it won’t replicate to the server & other clients

2 Likes

I tried in the leaderstats script but it said WaitForChild() is not a valid member…

1 Like

Hold on, is this a Script or a LocalScript then? If it is a Script, it doesn’t work because LocalPlayer isn’t defined on the server

1 Like

this is a local script (ignore)

1 Like

ServerScriptService.leaderstats:23: attempt to index nil with ‘WaitForChild’ this happens

Can you please post what the leaderstats script contains? I can’t do anything without knowing what might be causing the problem is

this is the whole script:

local player = game.Players.LocalPlayer 

game.Players.PlayerAdded:Connect(function(plr)
	local lstats = Instance.new("Folder")
	lstats.Name = "leaderstats"
	lstats.Parent = plr
	
	local exp = Instance.new("IntValue", plr)
	exp.Name = "exp"
	exp.Value = 0
	
	local lvl = Instance.new("IntValue", plr)
	lvl.Name = "lvl"
	lvl.Value = 1
	
	local e = Instance.new("IntValue", plr)
	e.Name = "expneeded"
	e.Value = 100
	
	
end)

local st = game.Players.LocalPlayer:WaitForChild("exp")
local player = game.Players.LocalPlayer
local function expFunc()
	if st.Value >= player.expneeded.Value then
		player.exp.Value -= player.expneeded.Value
		player.lvl.Value += 1
		player.expneeded.Value = player.expneeded.Value * player.lvl.Value
		if st.Value >= player.expneeded.Value then
			expFunc()
		end
	end
	expFunc()
	print("yes")
end

Why did you put the other part out of the PlayerAdded event?

game.Players.PlayerAdded:Connect(function(plr)
	local lstats = Instance.new("Folder")
	lstats.Name = "leaderstats"
	lstats.Parent = plr

	local exp = Instance.new("IntValue", lstats)
	exp.Name = "exp"
	exp.Value = 0

	local lvl = Instance.new("IntValue", lstats)
	lvl.Name = "lvl"
	lvl.Value = 1

	local e = Instance.new("IntValue", lstats)
	e.Name = "expneeded"
	e.Value = lvl.Value * 100

	local debounce = false -- to prevent the function from being repeatedly spammed due to being connected to a .Changed event

	local function expChanged()
		if not debounce then
			debounce = true
			repeat
				if exp.Value >= e.Value then
					exp.Value -= e.Value
					lvl.Value += 1
					e.Value = 100 * lvl.Value
					if exp.Value < e.Value then
						break
					end
				end
				print("yes")
				task.wait(0.1)
			until exp.Value < e.Value

			debounce = false
		end
	end
	
	local function lvlChanged()
		if not debounce then
			e.Value = 100 * lvl.Value
		end
	end

	exp.Changed:Connect(expChanged)
	lvl.Changed:Connect(lvlChanged)
end)

i don’t think that you can use game.Players.LocalPlayer in server scripts

That’s right, on the server LocalPlayer is nil because the server is running the script, not a client

yeah ,no
image

I didn’t add a level cap because you didn’t ask for it, you should have specified that you needed a 100 level cap

not lvl cap , I need exp when 100 lvl 2 you know?

I am going to break something
image

Seems like that’s an issue on the client, can you please show the piece of code that changes the level bar’s filling size?

while wait() do 
	script.Parent.Size = UDim2.new(game.Players.LocalPlayer.exp.Value*0.0025,0,0.05,0)
end

this changes it.