Table.insert overwriting previous table values

I am trying to make a racing game where players run around a track and collect items to make them go faster but i wanted to make a system that checks and says what place you are in and changes as you progress and i decided to put checkpoints around the map and when you touch it it adds a point to a value in your player.

now my problem occurs when i try to compare the values by placing them all in a table using table.insert and table.insert overwrites the previous value.

i have searched previous solutions on the forums but unfortunately none of them seem to work.
I’m a beginner at scripting any fed back on the script that i wrote would be appreciated.
– edit
i have updated to current

here is the code

local places = {
	first = 0,
	second = 0,
	third = 0,
	fourth = 0,
	fifth = 0,
	sixth = 0,
	seventh = 0,
	eighth = 0

}
local plrPlace = {
	firstPlr = 0,
	secondPlr = 0,
	thirdPlr = 0,
	fourthPlr = 0,
	fifthPlr = 0,
	sixthPlr = 0,
	seventhPlr = 0,
	eighthPlr = 0
} 
while wait()do
	
	local Vals = game.Player.points:GetChildren()

	for _ ,value in pairs(Vals) do
		local NV = value.Value
		table.insert(places,1,NV)
		table.sort(places)
		local plrs = game.Players:GetChildren()
		for _,plr in pairs(plrs)do
			if	    plr.Game.Point.Value >= places.first then
				places.first = plr.Game.Point.Value
			elseif	plr.Game.Point.Value >= places.second then
				places.second = plr.Game.Point.Value
			elseif plr.Game.Point.Value >= places.third then
				places.third = plr.Game.Point.Value
			elseif plr.Game.Point.Value >= places.fourth then
				places.fourth = plr.Game.Point.Value
			elseif plr.Game.Point.Value >= places.fifth then
				places.fifth = plr.Game.Point.Value
			elseif plr.Game.Point.Value >= places.sixth then
				places.sixth = plr.Game.Point.Value
			elseif plr.Game.Point.Value >= places.seventh then
				places.seventh = plr.Game.Point.Value
			elseif plr.Game.Point.Value >= places.eighth then
				places.eighth = plr.Game.Point.Value
			end

			for _ ,value in pairs(Vals) do
				local NV = value.Value
				table.insert(plrPlace,1,NV)
				table.sort(plrPlace)
				local plrs = game.Players:GetChildren()
			if  plr.Game.Point.Value == places.first then
				plrPlace.firstPlr = plr
				print("done")
			elseif   plr.Game.Point.Value == places.second then
				plrPlace.secondPlr = plr
				print("done2")
			elseif   plr.Game.Point.Value == places.third then
				plrPlace.thirdPlr = plr
			elseif   plr.Game.Point.Value == places.fourth then
				plrPlace.fourthPlr = plr
			elseif   plr.Game.Point.Value == places.fifth then
				plrPlace.fifthPlr = plr
			elseif   plr.Game.Point.Value == places.sixth then
				plrPlace.sixthPlr = plr
			elseif   plr.Game.Point.Value == places.seventh then
				plrPlace.seventhPlr = plr
			elseif   plr.Game.Point.Value == places.eighth then
				plrPlace.eighthPlr = plr
			end		
           end
		end
		
end
	```

What in the world…:scream_cat:

You should use the table.sort function and instead save the player’s place in an array.

local scores = game.Players:GetPlayers()
table.sort(scores, function(a, b)
    return a.Game.Point.Value > b.Game.Point.Value
end)

-- and here is how you would access the first player or the last
local first = scores[1]
local last = scores[#scores]

Also not sure where you are using table.insert. I don’t see that in your code.

my apologies that was the code i started to rewrite but didnt finish i updated to the original

You should be able to do what I just posted still.

1 Like

Sorry for the late reply i haven’t had much time to work on my game but i have implemented what u said but it still isn’t working. btw thanks for the assistance

table.insert(places,1,NV)

When you’re inserting new entries into the table you’re selecting the same index each time within the generic for loop, so with each new item inserted the item which was previously located at that index is overridden. Instead declare an upvalue to the generic for loop and use that as a counter such as in the following.

while wait()do
	
	local Vals = game.Player.points:GetChildren()
	local counter = 0

	for _ ,value in pairs(Vals) do
        counter = counter + 1
		local NV = value.Value
		table.insert(places,counter,NV)
		table.sort(places)
		local plrs = game.Players:GetChildren()
		for _,plr in pairs(plrs)do
1 Like