PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 04:28 PM |
I'm trying to make a table so that it will eventually give who has the most kills in a server. This isn't right, but this is what I have so far.
for _, player in pairs(game.Players:GetPlayers()) do local l = player["leaderstats"] for index, child in pairs(l:GetChildren()) do print(math.max(index, child.KOs.Value)) end end
how should I go about so I can continue this? |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
|
| 19 Aug 2015 04:29 PM |
for _, player in pairs(game.Players:GetPlayers()) do local l = player["leaderstats"] for index, child in pairs(l:GetChildren()) do print(math.max(index, child.KOs.Value)) end end
id recommend changing l to a different variable |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
|
| 19 Aug 2015 04:39 PM |
local function getDeadliestPlayer()
local function gatherKillsData() local killsTab = {} for i, v in ipairs(game.Players:GetPlayers()) do local ls = v:FindFirstChild("leaderstats") if ls then local kills = ls:FindFirstChild("Kills") if kills then table.insert(killsTab, kills.Value) end end end return killsTab end
local totalKills = gatherKillsData() local mostKills = math.max(totalKills)
for i, v in ipairs(game.Players:GetPlayers()) do if v.leaderstats.Kills.Value == mostKills then return v end end
end
local deadlyPlayer = getDeadliestPlayer() print(deadlyPlayer.Name) |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 04:40 PM |
I'll try that out
Thanks a lot! |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 04:41 PM |
I got this from output:
17:41:08.001 - ServerScriptService.Most KOs/WOs/Points Script:18: bad argument #1 to 'max' (number expected, got table) 17:41:08.003 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 18 - local getDeadliestPlayer 17:41:08.004 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 28 17:41:08.005 - Stack End |
|
|
| Report Abuse |
|
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
|
| 19 Aug 2015 04:44 PM |
whoops, my bad forgot to call unpack
local mostKills = math.max(unpack(totalKills)) |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 04:46 PM |
I put that in again, but I got this:
ServerScriptService.Most KOs/WOs/Points Script:18: bad argument #1 to 'max' (number expected, got no value) 17:45:05.957 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 18 - local getDeadliestPlayer 17:45:05.957 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 28 17:45:05.958 - Stack End |
|
|
| Report Abuse |
|
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
|
| 19 Aug 2015 04:48 PM |
| then that means the table is empty, and you don't have a leaderstats or a Kills value in leaderstats |
|
|
| Report Abuse |
|
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
|
| 19 Aug 2015 04:51 PM |
put this line under the local totalKills line:
print(unpack(totalKills))
and tell me what it prints |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 06:30 PM |
local function getDeadliestPlayer()
local function gatherKillsData() local killsTab = {} for i, v in ipairs(game.Players:GetPlayers()) do local ls = v:FindFirstChild("leaderstats") if ls then local kills = ls:FindFirstChild("KOs") if kills then table.insert(killsTab, kills.Value) end end end return killsTab end
local totalKills = gatherKillsData() print(unpack(totalKills)) local mostKills = math.max(unpack(totalKills))
for i, v in ipairs(game.Players:GetPlayers()) do if v.leaderstats.Kills.Value == mostKills then return v end end
end
local deadlyPlayer = getDeadliestPlayer() print(deadlyPlayer.Name)
19:29:46.890 - ServerScriptService.Most KOs/WOs/Points Script:19: bad argument #1 to 'max' (number expected, got no value) 19:29:46.893 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 19 - local getDeadliestPlayer 19:29:46.893 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 29 19:29:46.894 - Stack End |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 19 Aug 2015 06:32 PM |
The easy method:
local players = game.Players:GetPlayers(); table.sort(players, function(val1, val2) return val1.KOs.Value < val2.KOs.Value; end);
print("The person with the most KOs is: " .. players[1].Name); |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 19 Aug 2015 06:33 PM |
Woops return val1.KOs.Value > val2.KOs.Value; |
|
|
| Report Abuse |
|
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
|
| 19 Aug 2015 06:34 PM |
lel, use cnt's
can't believe i didn't think of that |
|
|
| Report Abuse |
|
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
| |
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 06:37 PM |
I tried cnts, it gives off this:
(It's line 40 now because I just made insta's script a comment)
19:34:25.311 - ServerScriptService.Most KOs/WOs/Points Script:40: attempt to index field '?' (a nil value) 19:34:25.314 - Stack Begin 19:34:25.315 - Script 'ServerScriptService.Most KOs/WOs/Points Script', Line 40 19:34:25.316 - Stack End |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
PureVoid
|
  |
| Joined: 25 Dec 2011 |
| Total Posts: 2624 |
|
|
| 19 Aug 2015 06:39 PM |
| print("The person with the most KOs is: " .. players[1].Name); |
|
|
| Report Abuse |
|
|
instawin
|
  |
| Joined: 04 Jun 2013 |
| Total Posts: 8777 |
|
|
| 19 Aug 2015 06:41 PM |
make sure you yield until a player has joined the server at least, if this is literally your whole script..
game.Players.PlayerAdded:wait() |
|
|
| Report Abuse |
|
|
Darkenus
|
  |
| Joined: 17 Jul 2014 |
| Total Posts: 1997 |
|
|
| 19 Aug 2015 06:42 PM |
Cnt Why do you like to put semicolons after your code. This isnt Java/C++/etc. Don't you find it tedious? |
|
|
| Report Abuse |
|
|