Blue Flag script for a racing game that tells a player he is being lapped

I have been trying to get this script to work, but so far it can only tell when another player is close to you, whenever i add the lines of code that take into account the amount of laps each player has, it doesnt work… this is the script so far

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
1 Like

Can you define “doesn’t work”.

Does the script error and stop running? If so, what line and what’s the error?

Does nothing happen and no output is given? If so try putting print statements for some of your if conditions to check that the inputs and output are as expected. E.g. if you have if di < dist then try doing print( di, dist, di < dist ) to check the values and the condition are coming out as you’d expect them to for a particular situation.

It’s hard to give specific help without an error and without you pointing towards the specific line that you think isn’t set up correctly

the specific line that isnt set up properly, or well the specific lines i should day, is everything that mentions laps, these lines:

local lapsCompleted = v.leaderstats.laps.Value

if laps.Value < lapsCompleted then Flag.Enabled = true

if i delete that and the if, and move the flag.enabled = true under the distance if, it shows the gui when another player is behind you regardless of how many laps the other person has