of     1   

LukeGabrieI
#228370364Sunday, December 03, 2017 8:29 AM GMT

I'm trying to think of a way for my game, that whenever someone has the most "points", I'll make a script that'll say: [Person Name] is the MVP for the Red Team and [Other Person Name] is the MVP for the Blue Team. I was wondering how I would be able to implement a script like that. I was thinking along the lines of #code for i,v in pairs(game.Players:GetPlayers()) do but idk what to put after that. Thanks, LukeGabrieI
Soybeen
#228370707Sunday, December 03, 2017 8:44 AM GMT

Each player has a leaderstat named Points, right? And you want the top for each team? Simply done: function TopPlayers(team) local winners = {} local board = {} for _,v in next,team:GetPlayers() do board[#board+1] = {v,v.leaderstats.Points.Value} end table.sort(board, function(a, b) return a[2] > b[2] end) local highest = board[#board][2] for _,v in next,board do if v[2] == highest then winners[#winners] = v[1] end end return winners,highest end) function EndRound() local message = Instance.new("Message",workspace) local redWinners,redHighest = TopPlayers(game.Teams.RedTeam) -- insert your team here local blueWinners,BlueHighest = TopPlayers(game.Teams.BlueTeam) for _,v in next,game.Players:GetPlayers() do v.leaderstats.Points.Value = 0 end message.Text = "Red Team MVP(s)"..table.concat(redWinners,", ").." with "..redHighest") wait(3) message.Text = "Blue Team MVP(s)"..table.concat(blueWinners,", ").." with "..blueHighest") wait(3) message:Destroy() end -- whenever you want to display the top winners, say EndRound()
Soybeen
#228370725Sunday, December 03, 2017 8:45 AM GMT

Replace this message.Text = "Red Team MVP(s)"..table.concat(redWinners,", ").." with "..redHighest") wait(3) message.Text = "Blue Team MVP(s)"..table.concat(blueWinners,", ").." with "..blueHighest") With this message.Text = "Red Team MVP(s): "..table.concat(redWinners,", ").." with "..redHighest.." points!" wait(3) message.Text = "Blue Team MVP(s): "..table.concat(blueWinners,", ").." with "..blueHighest.." points!"
LukeGabrieI
#228370814Sunday, December 03, 2017 8:50 AM GMT

thank you so much! (:
Soybeen
#228370899Sunday, December 03, 2017 8:54 AM GMT

Yep, also change BlueHighest to blueHighest
LukeGabrieI
#228392582Sunday, December 03, 2017 7:17 PM GMT

Thanks for the script, but I've had trouble trying to get the Player's Name to pop up lol after the game. It'll only say Blue Team MVP(s): with [insert number] points! It won't say the player's name.
Soybeen
#228411091Monday, December 04, 2017 1:48 AM GMT

Change this line winners[#winners] = v[1] To this winners[#winners] = v[1].Name
TaaRt
#228412042Monday, December 04, 2017 2:11 AM GMT

"for _,v in next,team:GetPlayers()" Isn't pairs faster than this? Pairs will return next() for each index BEFORE executing the content of the loop itself rather than calling next each iteration
128Gigabytes
#228412103Monday, December 04, 2017 2:13 AM GMT

No, just straight using next is faster. Also soy do you always write your code unindented or just on the forums?
TaaRt
#228413030Monday, December 04, 2017 2:35 AM GMT

-- theList is defined as a list of numbers 1 to 1024 function timeExecution(func) local now = os.clock() func() return os.clock() - now end func1 = function() local theList = theList for _,v in pairs(theList) do v = v + 1 print(v) end end func2 = function() local theList = theList for _,v in next,theList do v = v + 1 print(v) end end print(timeExecution(func1),timeExecution(func2)) I get results where either is faster each try, so they appear to give equal results
Soybeen
#228416771Monday, December 04, 2017 4:35 AM GMT

Yeah I don't indent my code. I turned off AutoIndent because the automatic ends broke my workflow and I just never cared about indenting either.
128Gigabytes
#228417023Monday, December 04, 2017 4:45 AM GMT

I just did my own test and you are right, the flip flop between being slightly faster than each other. I think I mistook pairs and ipairs (next and pairs consistently beat ipairs in speed) local tick = tick local list = {} for x = 1, 1000, 1 do list[x] = x; end do local start = tick() for x = 1, 10000, 1 do for _, _ in next, list do end end print(tick() - start) end do local start = tick() for x = 1, 10000, 1 do for _, _ in pairs(list) do end end print(tick() - start) end
cabbler
#228418325Monday, December 04, 2017 5:44 AM GMT

There is virtually 0 speed difference between next and pairs- ipairs too. Stop worrying over this crap.
128Gigabytes
#228418907Monday, December 04, 2017 6:16 AM GMT

No one is 'worried' but there are certain things you can make where knowing whats slightly faster matters, example I recently made a program that would convert a file into an image by encoding its data into the pixels rgb values, some files had billions of bytes so by optimizing the loops in similar ways to what we are talking about here I was able to make the program finish creating the image about 5 minutes faster on large files.
cabbler
#228419028Monday, December 04, 2017 6:22 AM GMT

Cool that's not relevant to this thread or even roblox development by the sounds of it.
128Gigabytes
#228419696Monday, December 04, 2017 7:01 AM GMT

Its relevant to why someone would want to know what different things are technically more efficient, not because the time matters but because how taxing on hardware they are matters, especially on roblox because although they have improved greatly, we all know the roblox engine lags a lot when you have a lot going on. And again, no one is worried about it we were just having an interesting discussion about the way the different loops work, you are acting like we thought it was an issue.
cabbler
#228421893Monday, December 04, 2017 9:45 AM GMT

nah not relevant, and if you weren't concerned you wouldn't have benchmarked. just take the advice get over this stuff.
TaaRt
#228422821Monday, December 04, 2017 11:14 AM GMT

If we're speaking of relevance then even the rules of the forum disagree with the forum description (anything that is a help topic vs anything that is not a help topic). I reckon any food for thought related to Lua is relevant @128 glad you get the same benchmarks, I was kind of curious whether I would've messed up somehow

    of     1