Advertisement
artucox7

KillCount

Apr 22nd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. script.Parent = game.Workspace
  2.  
  3. wait(1)
  4.  
  5. function onHumanoidDied(humanoid, player)
  6. local stats = player:findFirstChild("leaderstats")
  7. if stats ~= nil then
  8. local deaths = stats:findFirstChild("Deaths")
  9. deaths.Value = deaths.Value + 1
  10.  
  11. -- do short dance to try and find the killer
  12.  
  13. local killer = getKillerOfHumanoidIfStillInGame(humanoid)
  14.  
  15. handleKillCount(humanoid, player)
  16. end
  17. end
  18.  
  19. function onPlayerRespawn(property, player)
  20. -- need to connect to new humanoid
  21.  
  22. if property == "Character" and player.Character ~= nil then
  23. local humanoid = player.Character.Humanoid
  24. local p = player
  25. local h = humanoid
  26. humanoid.Died:connect(function() onHumanoidDied(h, p) end )
  27. end
  28. end
  29.  
  30. function getKillerOfHumanoidIfStillInGame(humanoid)
  31. -- returns the player object that killed this humanoid
  32. -- returns nil if the killer is no longer in the game
  33.  
  34. -- check for kill tag on humanoid - may be more than one - todo: deal with this
  35. local tag = humanoid:findFirstChild("creator")
  36.  
  37. -- find player with name on tag
  38. if tag ~= nil then
  39.  
  40. local killer = tag.Value
  41. if killer.Parent ~= nil then -- killer still in game
  42. return killer
  43. end
  44. end
  45.  
  46. return nil
  47. end
  48.  
  49. function handleKillCount(humanoid, player)
  50. local killer = getKillerOfHumanoidIfStillInGame(humanoid)
  51. if killer ~= nil then
  52. local stats = killer:findFirstChild("leaderstats")
  53. if stats ~= nil then
  54. local kills = stats:findFirstChild("Kills")
  55. if killer ~= player then
  56. kills.Value = kills.Value + 1
  57.  
  58. else
  59. kills.Value = kills.Value - 1
  60.  
  61. end
  62. end
  63. end
  64. end
  65.  
  66.  
  67. -----------------------------------------------
  68.  
  69.  
  70.  
  71. function findAllFlagStands(root)
  72. local c = root:children()
  73. for i=1,#c do
  74. if (c[i].className == "Model" or c[i].className == "Part") then
  75. findAllFlagStands(c[i])
  76. end
  77. if (c[i].className == "FlagStand") then
  78. table.insert(stands, c[i])
  79. end
  80. end
  81. end
  82.  
  83. function hookUpListeners()
  84. for i=1,#stands do
  85. stands[i].FlagCaptured:connect(onCaptureScored)
  86. end
  87. end
  88.  
  89. function onPlayerEntered(newPlayer)
  90.  
  91. if CTF_mode == true then
  92.  
  93.  
  94. local stats = Instance.new("IntValue")
  95. stats.Name = "leaderstats"
  96.  
  97. local kills = Instance.new("IntValue")
  98. kills.Name = "Kills"
  99. kills.Value = 0
  100.  
  101. local deaths = Instance.new("IntValue")
  102. deaths.Name = "Deaths"
  103. deaths.Value = 0
  104.  
  105. local captures = Instance.new("IntValue")
  106. captures.Name = "Captures"
  107. captures.Value = 0
  108.  
  109. kills.Parent = stats
  110. deaths.Parent = stats
  111. captures.Parent = stats
  112.  
  113. -- VERY UGLY HACK
  114. -- Will this leak threads?
  115. -- Is the problem even what I think it is (player arrived before character)?
  116. while true do
  117. if newPlayer.Character ~= nil then break end
  118. wait(5)
  119. end
  120.  
  121. local humanoid = newPlayer.Character.Humanoid
  122.  
  123. humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
  124.  
  125. -- start to listen for new humanoid
  126. newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
  127.  
  128.  
  129. stats.Parent = newPlayer
  130.  
  131. else
  132.  
  133. local stats = Instance.new("IntValue")
  134. stats.Name = "leaderstats"
  135.  
  136. local kills = Instance.new("IntValue")
  137. kills.Name = "Kills"
  138. kills.Value = 0
  139.  
  140. local deaths = Instance.new("IntValue")
  141. deaths.Name = "Deaths"
  142. deaths.Value = 0
  143.  
  144. kills.Parent = stats
  145. deaths.Parent = stats
  146.  
  147. -- VERY UGLY HACK
  148. -- Will this leak threads?
  149. -- Is the problem even what I think it is (player arrived before character)?
  150. while true do
  151. if newPlayer.Character ~= nil then break end
  152. wait(5)
  153. end
  154.  
  155. local humanoid = newPlayer.Character.Humanoid
  156.  
  157. humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
  158.  
  159. -- start to listen for new humanoid
  160. newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
  161.  
  162.  
  163. stats.Parent = newPlayer
  164.  
  165. end
  166.  
  167. end
  168.  
  169.  
  170. function onCaptureScored(player)
  171.  
  172. local ls = player:findFirstChild("leaderstats")
  173. if ls == nil then return end
  174. local caps = ls:findFirstChild("Captures")
  175. if caps == nil then return end
  176. caps.Value = caps.Value + 1
  177.  
  178. end
  179.  
  180.  
  181. findAllFlagStands(game.Workspace)
  182. hookUpListeners()
  183. if (#stands > 0) then CTF_mode = true end
  184. game.Players.ChildAdded:connect(onPlayerEntered)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement