Advertisement
1m1m0

Basic Leaderstats

Jan 1st, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | Source Code | 0 0
  1. -- Basic Leaderstats --
  2.  
  3. function onXPChanged(player, XP, level)
  4.     if XP.Value>=level.Value * 10 then -- Lv 1 to Lv 2 need 20 XP
  5.         XP.Value = 0 -- If you do not want the Xp turn to 0 when lv up , delete this line
  6.         level.Value = level.Value + 1
  7.     end
  8. end
  9.  
  10. -- USELESS (Unless used otherwise) --
  11.  
  12. function onLevelUp(player, XP, level)
  13.     local m = Instance.new("Hint")
  14.     m.Parent = game.Workspace
  15.     m.Text = player.Name .. " Leveled up!" -- the message display when player lv up
  16.     wait()
  17.     m.Parent = nil
  18.    player.Humanoid.Health = 0
  19. end
  20.  
  21. -------------------------------------
  22.  
  23. function onPlayerRespawned(player)
  24.     wait(5)
  25.  
  26.     player.Character.Humanoid.Health = player.Character.Humanoid.Health + player.leaderstats.Level * 10
  27.  
  28.     player.Character.Humanoid.MaxHealth = player.Character.Humanoid.MaxHealth + player.leaderstats.Level * 10
  29.  
  30. end
  31.  
  32. function onPlayerEntered(newPlayer)
  33.    
  34.     local stats = Instance.new("IntValue")
  35.     stats.Name = "leaderstats"
  36.     local stats2 = Instance.new("IntValue")
  37.     stats2.Name = "Tycoon"
  38.  
  39.  
  40.     local cash = Instance.new("IntValue")
  41.     cash.Name = "Coins"                 --Change "Cash" to change the currency name.
  42.     cash.Value = 0          --Change the 0 to how much you want people to start with
  43.  
  44.    local kills = Instance.new("IntValue")
  45.     kills.Name = "Kills"
  46.     kills.Value = 0
  47.  
  48.     local deaths = Instance.new("IntValue")
  49.     deaths.Name = "Deaths"
  50.     deaths.Value = 0
  51.  
  52.    local level = Instance.new("IntValue")
  53.    level.Name = "Level" -- The Name of LV
  54.    level.Value = 1
  55.  
  56.    local xp = Instance.new("IntValue")
  57.    xp.Name = "XP" -- The Name of XP
  58.    xp.Value = 0
  59.    
  60.    cash.Parent = stats
  61.     stats2.Parent = newPlayer  
  62.     stats.Parent = newPlayer
  63.     kills.Parent = stats
  64.     deaths.Parent = stats
  65.    level.Parent = stats
  66.    xp.Parent = stats
  67.  
  68.     xp.Changed:connect(function() onXPChanged(newPlayer, xp, level) end)
  69.     level.Changed:connect(function() onLevelUp(newPlayer, xp, level) end)
  70.  
  71.     while true do
  72.         if newPlayer.Character ~= nil then break end
  73.         wait(5)
  74.     end
  75.  
  76.     local humanoid = newPlayer.Character.Humanoid
  77.  
  78.     humanoid.Died:connect(function() onHumanoidDied(humanoid, newPlayer) end )
  79.  
  80.     -- start to listen for new humanoid
  81.     newPlayer.Changed:connect(function(property) onPlayerRespawn(property, newPlayer) end )
  82.  
  83.  
  84.     stats.Parent = newPlayer
  85.  
  86. end
  87.  
  88. -- Killfeed (Requires GUI) --
  89.  
  90. function Send_DB_Event_Died(victim, killer)
  91.     -- killer may be nil
  92.     local killername = "unknown"
  93.     if killer ~= nil then killername = killer.Name end
  94.     print(victim.Name, " was killed by ", killername)
  95.  
  96.     if shared["deaths"] ~= nil then
  97.         shared["deaths"](victim, killer)
  98.         print("Death event sent.")
  99.     end
  100. end
  101.  
  102. function Send_DB_Event_Kill(killer, victim)
  103.     print(killer.Name, " killed ", victim.Name)
  104.     if shared["kills"] ~= nil then
  105.         shared["kills"](killer, victim)
  106.         print("Kill event sent.")
  107.     end
  108. end
  109.  
  110. -----------------------------
  111.  
  112. function onHumanoidDied(humanoid, player)
  113.     local stats = player:findFirstChild("leaderstats")
  114.     if stats ~= nil then
  115.         local deaths = stats:findFirstChild("Deaths")
  116.         deaths.Value = deaths.Value + 1
  117.  
  118.         local killer = getKillerOfHumanoidIfStillInGame(humanoid)
  119.        
  120.         Send_DB_Event_Died(player, killer)
  121.         handleKillCount(humanoid, player)
  122.     end
  123. end
  124.  
  125. function onPlayerRespawn(property, player)
  126.     -- need to connect to new humanoid
  127.    
  128.     if property == "Character" and player.Character ~= nil then
  129.         local humanoid = player.Character.Humanoid
  130.             local p = player
  131.             local h = humanoid
  132.             humanoid.Died:connect(function() onHumanoidDied(h, p) end )
  133.     end
  134. end
  135.  
  136. function getKillerOfHumanoidIfStillInGame(humanoid)
  137.     -- returns the player object that killed this humanoid
  138.     -- returns nil if the killer is no longer in the game
  139.  
  140.     -- check for kill tag on humanoid - may be more than one - todo: deal with this
  141.     local tag = humanoid:findFirstChild("creator")
  142.  
  143.     -- find player with name on tag
  144.     if tag ~= nil then
  145.        
  146.         local killer = tag.Value
  147.         if killer.Parent ~= nil then -- killer still in game
  148.             return killer
  149.         end
  150.     end
  151.  
  152.     return nil
  153. end
  154.  
  155. function handleKillCount(humanoid, player)
  156.     local killer = getKillerOfHumanoidIfStillInGame(humanoid)
  157.     if killer ~= nil then
  158.         local stats = killer:findFirstChild("leaderstats")
  159.         if stats ~= nil then
  160.             local kills = stats:findFirstChild("Kills")
  161.            local cash = stats:findFirstChild("Coins")
  162.          local xp = stats:findFirstChild("XP")
  163.             if killer ~= player then
  164.                 kills.Value = kills.Value + 1
  165.             cash.Value = cash.Value + 25
  166.             xp.Value = xp.Value + 5
  167.                
  168.             else
  169.                 kills.Value = kills.Value - 0
  170.                
  171.             end
  172.             Send_DB_Event_Kill(killer, player)
  173.         end
  174.     end
  175. end
  176.  
  177. game.Players.ChildAdded:connect(onPlayerEntered)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement