|
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
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
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() |
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
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!" |
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
Yep, also change BlueHighest to blueHighest |
|
|
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.
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
Change this line
winners[#winners] = v[1]
To this
winners[#winners] = v[1].Name |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
"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 |
|
|
No, just straight using next is faster.
Also soy do you always write your code unindented or just on the forums? |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
-- 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 |
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
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. |
|
|
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 |
|
cabblerJoin Date: 2015-06-19 Post Count: 735 |
There is virtually 0 speed difference between next and pairs- ipairs too. Stop worrying over this crap. |
|
|
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. |
|
cabblerJoin Date: 2015-06-19 Post Count: 735 |
Cool that's not relevant to this thread or even roblox development by the sounds of it. |
|
|
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. |
|
cabblerJoin Date: 2015-06-19 Post Count: 735 |
nah not relevant, and if you weren't concerned you wouldn't have benchmarked. just take the advice get over this stuff. |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
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 |
|