Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

table manipulation of finding the greatest value?

okay sorry I'm bad at wording questions :P

okay so basically I'm working on a kill script that gives a kill and as assist. Inside the player (depending on how many times the player has been hit by different people) are folders that contain two values : the player, and how much damage they received. so basically, I want to get all the damage values, and I want to see which one is the 2nd biggest, that way I can find out who to give an assist or critical assists to.

so I thought if I put it into a table it might be able to find the biggest value, however I need to someone pair the damage value back to the correct player :( Here is my failed attempt:

			local tag = player:GetChildren()

				for i = 1,#tag do
					if tag[i].Name == "FolderTag" then
						local damages = {}
						table.insert(damages,tag[i].damage.Value) -- damage value
						local people = {}
						table.insert(people,tag[I].creator.value)  --player value
					end
				end

				--and then code down here that finds the one with the greatest damage xD(and returns the player I guess)

sorry if this is wordy but I tried to explain it my best, thanks for any help,

~Bubs

1 answer

✓ Accepted answer

table.sort

You can pair up the tables and provide table.sort with a custom function for sorting them

local tag = player:GetChildren()
local t = {}

    for i = 1,#tag do
        if tag[i].Name == "FolderTag" then
            table.insert(t, tag[i])
        end
    end
	table.sort(t, function(a, b) return a.damage.Value > b.damage.Value end);
	-- t[1].creator.Value is the person who did the most damage (At once)
	-- t[2].creator.Value did the 2nd most, etc etc

1 comment

0

brilliant , thanks man ! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.