Need help with a 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

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

1 Like

@foxsonic55, Try to make a invisible part and if the player touches him he will have made one more round:

local LapsPart = workspace.LapsPart
LapsPart.Touched:Connect(function(hit)
    game.Players.LocalPlayer.leaderstats.laps.Value = game.Players.LocalPlayer.leaderstats.laps.Value + 1
end)

Why:

local di = (me.Character.Head.Position - tor.Position).magnitude

Just focus on one player, leave the other one out. Why do you want to compare the players between them?

Don’t use touched events, they have a lot of issues. Think more creatively since there are other methods of finding touching parts.

1 Like

Use positions? Exemple:

local goalZone = Vector3.new(0,0,5)
local max = 15
local HRP = player.Character.HumanoidRootPart
while wait(1) do
if HRP.Position.Z > goalZone.Z and HRP.Position < max then
print("You reached the GoalZone! Flag.enabled == true")
else
print("You dont reached the GoalZone! Flag.enabled == false ")
end
end

i can’t leave the other player out, then the script would be useless… pretty much what im trying to do is give a signal to a player that tells him another player with more laps is approaching him…

like i previously said, so far i have only managed to make a script that shows a signal when another player is close without taking laps into consideration, what i need help with is making it work only when the player that is approaching has more laps

1 Like
local Racestatus = true --if the Race is running
local Radius = 20
local LocalPlayer = nil
local NumberOfPlayers = 0
for index, player in pairs(game.Players.GetPlayers()) do
    if player ~= nil then
        NumberOfPlayers = NumberOfPlayers + 1
    end
end
game.Players.PlayerAdded:Connect(function(player)
    LocalPlayer = player
end)
while NumberOfPlayers > 0 and Racesatus do
   spawn(function()
   local AllPlayers = game.players:GetPlayers()
   for index, player in ipairs(AllPlayers) do
      local OtherPlayer = AllPlayers[index + 1]
      if not player.Name == LocalPlayer then
         print("Player "..player.Name.."is "..player:DistanceFromCharacter(OtherPlayer.Head.Position).."from"..OtherPlayer.Name.." away")
      end
      if player:DistanceFromCharacter(OtherPlayer.Head.Position) < Radius and OtherPlayer.leaderstats.laps.Value > LocalPlayer.leaderstats.laps.Value then
        print("Player "..OtherPlayer.Name.." comes with "..OtherPlayer.leaderstats.laps.Value.." laps more than you")
      end
   end
end)
end
2 Likes

Couple of correction suggestions for your code.

  • If done from a LocalScript, LocalPlayer is implicit. The first line is not necessary and it will infact register the LocalPlayer variable as other players when they join, so you should never do this.

  • Don’t use while wait loops.

  • game.players will throw an error because players is not a valid member of the DataModel. Use Players or game:GetService(“Players”) (canonical way to fetch a service).

  • GetPlayers generates an array; use ipairs to iterate through it. You should also not assume truthy values in your code (OtherPlayer is looked up at index++ which can be nil especially for the last iterated element of the array).

1 Like

Thx, i dont tested my code. Ok i will edit it. It is from a ServerScript. I write Players and not players.
I want to read the website in the in pairs and ipairs (and key, I read it a long time ago but can’t find it again)

@foxsonic55, dont spam or you will reported:
https://devforum.roblox.com/t/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/377702/2

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

does that count as spam?.. i mean the first post is different from the other two and each post is separated by atleast a week, its not like im posting this every hour… i didnt get help so i decided to try again after a week… if it is spam then im sorry, i didnt know and i wont do that again

1 Like

@foxsonic55, have my code help you or not?

i havent tried it, im not home atm… but from what i can see this may be it… thanks for your help

1 Like

alright, just tried it and it isn’t working… first i just pasted the script in, fixed a grammar issue (line 13), placed it into the seats of two cars and published the game… i brought in a friend, he did a few laps more than me and when i checked the dev console nothing was printed… afterwards i decided to add all the lines of code that show the blue flag gui to see if that would change something, pasted the new script into the two car seats and tried again, same result, no gui and no printed message

1 Like
local Racestatus = true --if the Race is running then it is true
local Radius = 20
local LocalPlayer = nil
local NumberOfPlayers = 0
for index, player in pairs(game.Players.GetPlayers()) do
    if player ~= nil then
        NumberOfPlayers = NumberOfPlayers + 1
    end
end
game.Players.PlayerAdded:Connect(function(player)
    LocalPlayer = player
end)
while Racestatus == true do
   wait(2)
   local AllPlayers = game.players:GetPlayers()
   local OtherPlayer = nil
   for index, player in ipairs(AllPlayers) do

      if AllPLayers[player + 1]  ~= nil then
          OtherPlayer = AllPlayers[player + 1] 
          print(AllPlayers[player + 1].Name)--if this print something, then let me know, else here is a error
      else
          OtherPlayer = AllPlayers[player - 1]
           print(AllPlayers[player - 1].Name)
      end

      if not player.Name == LocalPlayer then
         print("Player "..player.Name.."is "..player:DistanceFromCharacter(OtherPlayer.Head.Position).."from"..OtherPlayer.Name.." away")
      end
      if player:DistanceFromCharacter(OtherPlayer.Head.Position) < Radius and OtherPlayer.leaderstats.laps.Value > LocalPlayer.leaderstats.laps.Value then
        print("Here its WORK!!! Epic LOL.")
        print("Player "..OtherPlayer.Name.." comes with "..OtherPlayer.leaderstats.laps.Value.." laps more than you")
      end
   end
end

I try with replace AllPlayers[index + 1] with AllPlayers[player + 1]. It should work (I hope it).

nope i tried with the new script and same result as before, its still not working

1 Like