Help with simple math :D

local AllPositions = FindAllPositions()
	local AmountOfRacers = #game.Players:GetPlayers()
	for _,currentPlayer in pairs(game.Players:GetPlayers()) do
		local PositionOfThisPlayer = FindPlayerPosition(currentPlayer)
		local LapOfThisPlayer = FindPlayerLap(currentPlayer)
		--for i,v in pairs(AllPositions) do
			--if (PositionOfThisPlayer == v) then
					
			--end
		--end
		local MaxValue = math.min(table.unpack(AllPositions)) -- 1 player position
		local PlayerPos = math.ceil((PositionOfThisPlayer))%MaxValue)
		table.insert(UI,{Player = currentPlayer,Position = PlayerPos})
	end 

Hi guys, that’s basically a script I am making that basically works by racing, the one that is first, wins, I am using ProgressTracker by @Zomebody
It basically returns a Number from 0 to 1 :smiley:

Code works perfectly, but, how could I add into this, more value to the player that has more Lap?
‘LapOfThisPlayer’ is the lap player is using :smiley:

1 Like

I don’t really understand what you’re asking, are you trying to figure out what player has the highest lap count?

A way to make player go first according to the lap

Hey, no I am trying to make the player goes first according to lap :smiley:
Like if Lap added more position value or smt but no, bc that would glitch it :smiley:

Is this Statement the way you want it. Because the variable name and the function you called are both controversial here.

hey, that’s actually working :smiley:

local AllPositions = FindAllPositions()
	local AmountOfRacers = #game.Players:GetPlayers()
	for _,currentPlayer in pairs(game.Players:GetPlayers()) do
		local PositionOfThisPlayer = FindPlayerPosition(currentPlayer)
		local LapOfThisPlayer = FindPlayerLap(currentPlayer)
		
		local MaxValue = math.max(table.unpack(AllPositions)) -- the pladyer that is going first
		local PlayerPos = math.ceil(((PositionOfThisPlayer)%MaxValue))
		PlayerPos = PlayerPos and PlayerPos or 0
		table.insert(UI,{Player = currentPlayer,Position = PlayerPos})
	end 

I changed and it works the same.

What I mean is; if player Lap (LapOfThisPlayer) is higher then other players, he gets next position, like in race games

1 Like

Your explanation is rough, but I think what you are looking for is table.sort()? Maybe something like…

local players = game.Players:GetPlayers()
table.sort(players, function(a, b)
  return FindPlayerLap(a) > FindPlayerLap(b)
end)

local MaxValue = math.max(table.unpack(FindAllPositions()))
for _, pl in pairs(players) do
  table.insert(UI, {Player = pl, Position = FindPlayerPosition(pl) % MaxValue})
end

This would insert players into the table ‘UI’ first if they have a higher ‘LapOfThisPlayer’. This is the best I can do based on your explanation + code. I’m not sure how ‘PlayerPosition’ factors into it at all.

Some side notes:

No need to do this inside the loop, the table ‘AllPositions’ is not changing. I have placed it outside the loop.

Pointless statement because the line above it will error if ‘PlayerPos’ is nil.

Haven’t looked at the Progress Tracker code, but if it is giving a decimal value from 0 to 1 that tells where a player is along the track (0=start, 1=end), then you should be able to just keep a separate lap counter for each player (in a table) and add that value to the Progress Tracker decimal (for a player) to get a total position score.

For example:
player 1: 0.5 around the track on lap 3
player 2: 0.75 around the track on lap 3
player 3: 0.25 around the track on lap 4

p1 total = 0.5 + 3 = 3.5
p2 total = 0.75 + 3 = 3.75
p3 total = 0.25 + 4 = 4.25

p3 is winning based on laps even though only 0.25 around the track. Once you have those total scores for all players you can sort to find who is in first, second, etc.

yes, but if I do that, then the Position of the player will be 3.5, and In a race that doesn’t makes sense

or, I might redo all, and use that as progress, and check who’s higher on that :smiley:

Hey, question, but wouldn’t table.sort try to loop only between 2 values?

It makes sense the table.sort tho, but how would that count by lap? or by who has highest points when lap’s 1

Right now it’s not D: (with your code)

Sorry for bad explaining.

So basically, the player that has higher lap has first position or if everyone is in same lap, the one that has better (FindPLayerPosition(pl)) that returns number from 0 to 1 with progress , is first

I tried this, but it doesn’t works neither so oof

	local players = game.Players:GetPlayers()
	for i,v in pairs(players) do
		RefreshPosition(v,Pos)
	end
	local MaxValue = math.max(table.unpack(FindAllPositions()))
	table.sort(players, function(a, b)
		return FindPlayerLap(a) < FindPlayerLap(b)
	end)
	for i,v in pairs(players) do
		table.insert(UI,{Player = v,Position = math.ceil(FindPlayerPosition(v)%MaxValue)})
	end

Also tried your code!

Alright now this is a description we can actually work with. So basically their position is either based on having higher lap count or it’s based on the return of ‘FindPlayerPosition’. You could do all of that in the table.sort:

local players = game.Players:GetPlayers()
table.sort(players, function(a, b)
  local aLapCount = FindPlayerLap(a)
  local bLapCount = FindPlayerLap(b)
  if aLapCount == bLapCount then
    return FindPlayerPosition(a) > FindPlayerPosition(b)
  else
    return aLapCount > bLapCount
  end
end)

for i, v in pairs(players) do
  table.insert(UI, {Player = v, Position = i})
end
2 Likes

Hey, ty for reply :smiley:

Well, I sorted it out ago like a hour.

local Players = game.Players:GetPlayers()
		table.sort(Players, function(a, b)	
			return FindPlayerPosition(a)  + FindPlayerLap(a) > FindPlayerPosition(b) + FindPlayerLap(b)
		end)
		for i,v in pairs(Players) do
			table.insert(UI,{Player = v,Position = i})
		end

But I’ll still mark you as solution since you helped me think about table.sort and give me idea to do this :smiley:

1 Like