Overhead Gui won't display when reaching certain leaderstat value

So I am new to scripting and am using this model for the overhead gui. I’m trying to modify it so that when a player reaches a certain amount of exp, they get a title, but the code won’t run.

Script:

local rep = game:GetService("ServerStorage")
local nametag = rep.NameTag

--Functions
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        --Varibles
        local head = char.Head
        local newtext = nametag:Clone()
        local uppertext = newtext.UpperText
        local lowertext = newtext.LowerText
        local humanoid = char.Humanoid
        
        humanoid.DisplayDistanceType = "None"
        
        --Main Text
        newtext.Parent = head
        newtext.Adornee = head
        uppertext.Text = player.Name
        
        -- Exp Title (this part won't work)
        local exp = player.leaderstats.Exp.Value
        
        if exp >= 0 then
            lowertext.Text = "Newbie (I) | 0+ exp"
            lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
            print("Gave Newbie (I)")
        end
        if exp >= 25 then
            lowertext.Text = "Newbie (II) | 25+ exp" 
            lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
            print("Gave Newbie (II)")
        end
        if exp >= 50 then
            lowertext.Text = "Newbie (III) | 50+ exp" 
            lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
            print("Gave Newbie (III)")
        end
    end)
end)

I’ve tried swapping exp with the value, ie if 0 >= exp then. This time it did display, but didn’t give me the correct title. It ran all the code and gave me the highest possible title when I didn’t have the required exp. For example, if I had 10 exp, it gave me “Newbie (III) | 50+ exp” instead of “Newbie (I) | 0+ exp”

Any help is appreciated. Thanks in advance!

I think it’s because when you wrote exp >= 0, it automatically went to 50 because it said if it’s greater than or equal to. I’m pretty sure this code will fix it:

        if exp >= 0 and exp < 25 then
            lowertext.Text = "Newbie (I) | 0+ exp"
            lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
            print("Gave Newbie (I)")
        end
        if exp >= 25 and exp < 50 then
            lowertext.Text = "Newbie (II) | 25+ exp" 
            lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
            print("Gave Newbie (II)")
        end
        if exp >= 50 then
            lowertext.Text = "Newbie (III) | 50+ exp" 
            lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
            print("Gave Newbie (III)")
        end
    end)
end)
--Ok, I tidied it up
local rep = game:GetService("ServerStorage")
local nametag = rep.NameTag

--Functions
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		--Variables
		local head = char.Head
		local newtext = nametag:Clone()
		local uppertext = newtext.UpperText
		local lowertext = newtext.LowerText
		local humanoid = char.Humanoid

		humanoid.DisplayDistanceType = "None"

		--Main Text
		newtext.Parent = head
		newtext.Adornee = head
		uppertext.Text = player.Name

		local function UpdateExp()
			local exp = player.leaderstats.Exp.Value
			if exp >= 0 then
				lowertext.Text = "Newbie (I) | 0+ exp"
				lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
				print("Gave Newbie (I)")
			end
			if exp >= 25 then
				lowertext.Text = "Newbie (II) | 25+ exp" 
				lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
				print("Gave Newbie (II)")
			end
			if exp >= 50 then
				lowertext.Text = "Newbie (III) | 50+ exp" 
				lowertext.TextColor3 = Color3.fromRGB(171, 171, 171)
				print("Gave Newbie (III)")
			end
		end
		
		player.leaderstats.Exp:GetPropertyChangedSignal("Value"):Connect(function()
			UpdateExp()
		end)
		UpdateExp()
	end)
end)

This worked, thank you! (putting this here cause character limit)

1 Like