I need help with a script that activates a gui whenever a player with a higher leaderboard value gets within a certain radius of the player

I’m currently working on a racing game, for those of you that aren’t familiar with the common flag system, there’s this thing called a blue flag that is shown to racers every time another that is a lap higher is behind them so they can let them pass without interfering… on my game i want to replicate this, i got the script to show the gui when another person is close, but i can’t get it to interact with the leaderboard here’s what i have so far:

me = game.Players.LocalPlayer
laps = me.leaderstats.laps
dist = 200
Flag = Instance.new(“ScreenGui”,me.PlayerGui)
Flag.Name = “Blue Flag”
bg = Instance.new(“ImageLabel”,Flag)
bg.Size = UDim2.new(0,200,0,200)
bg.Position = UDim2.new(0.15, 0, 0.8, -100)
bg.Image = “http://www.roblox.com/asset/?id=397459039
bg.BackgroundTransparency = 1
Flag.Enabled = false

while true do
wait()
for _,v in pairs(game.Players:GetPlayers()) do
if v.Name ~= me.Name then
if v.Character then
local tor = v.Character:findFirstChild(“Head”)
local lp = v.leaderstats.laps
if tor then
local di = (me.Character.Head.Position - tor.Position).magnitude
if di < dist then
if laps < lp then
Flag.enabled = true
end
end
if dist < di then
Flag.Enabled = false
end
end
end
end
end
end

image

Under the assumption you’re trying to recreate that if a car has been lapped, the code you’ve provided should work, you just need to look at the Value property of “laps” in the player’s leaderstats.

Please format code next time with ``` --code-- ```

    ...
    local lapsCompleted = v.leaderstats.laps.Value -- .Value
        if otherPlayerHead then
            local distance = (me.Character.Head.Position - tor.Position).magnitude
                if distance < dist then    -- X < 200
                    if laps.Value < lapsCompleted then    -- .Value
                        Flag.enabled = true
                    end
                    ...

so i did the changes and it still doesnt work, heres what it looks like

me = game.Players.LocalPlayer
dist = 200
Flag = Instance.new("ScreenGui",me.PlayerGui)
Flag.Name = "Blue Flag"
bg = Instance.new("ImageLabel",Flag)
bg.Size = UDim2.new(0,200,0,200)
bg.Position = UDim2.new(0.15, 0, 0.8, -100)
bg.Image = "http://www.roblox.com/asset/?id=397459039"
bg.BackgroundTransparency = 1
Flag.Enabled = false
laps = me.leaderstats.laps

while true do
	wait()
	for _,v in pairs(game.Players:GetPlayers()) do
		if v.Name ~= me.Name then
			if v.Character then
				local tor = v.Character:findFirstChild("Head")
				  local lapsCompleted = v.leaderstats.laps.Value 
				if tor then
					local di = (me.Character.Head.Position - tor.Position).magnitude
					if di < dist then
						
					   if laps.Value < lapsCompleted then 
						Flag.Enabled = true
						
					end
					end
					if dist <  di then
						Flag.Enabled = false
						end
					end
				end
			end
		end
	end ```