Advertisement
HowToRoblox

BountyServer

Jan 11th, 2023
1,562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.96 KB | None | 0 0
  1. --Data saving
  2. local dss = game:GetService("DataStoreService")
  3. local cashDS = dss:GetDataStore("PLAYERS DATA STORE")
  4.  
  5.  
  6. function saveData(plr)
  7.     if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  8.  
  9.         local cash = plr.leaderstats.Cash.Value
  10.  
  11.         local success, err = nil, nil
  12.         while not success do
  13.             success, err = pcall(function()
  14.                 cashDS:SetAsync(plr.UserId, cash)
  15.             end)
  16.             if err then
  17.                 warn(err)
  18.             end
  19.             task.wait(0.02)
  20.         end
  21.     end
  22. end
  23.  
  24. game.Players.PlayerRemoving:Connect(saveData)
  25. game:BindToClose(function()
  26.     for _, plr in pairs(game.Players:GetPlayers()) do
  27.         saveData(plr)
  28.     end
  29. end)
  30.  
  31. game.Players.PlayerAdded:Connect(function(plr)
  32.  
  33.     local dataFailedWarning = Instance.new("StringValue")
  34.     dataFailedWarning.Name = "DATA FAILED TO LOAD"
  35.     dataFailedWarning.Parent = plr
  36.  
  37.     local success, plrData = nil, nil
  38.     while not success do
  39.         success, plrData = pcall(function()
  40.             return cashDS:GetAsync(plr.UserId)
  41.         end)
  42.         task.wait(0.02)
  43.     end
  44.     dataFailedWarning:Destroy()
  45.    
  46.     if not plrData then
  47.         plrData = 0
  48.     end
  49.  
  50.     local ls = Instance.new("Folder")
  51.     ls.Name = "leaderstats"
  52.  
  53.     local cashVal = Instance.new("IntValue")
  54.     cashVal.Name = "Cash"
  55.     cashVal.Value = plrData
  56.     cashVal.Parent = ls
  57.    
  58.     local bountyVal = Instance.new("IntValue")
  59.     bountyVal.Name = "Bounty"
  60.     bountyVal.Value = 0
  61.     bountyVal.Parent = ls
  62.  
  63.     ls.Parent = plr
  64. end)
  65.  
  66.  
  67.  
  68. --Bounty handling
  69. local bountyPerKill = 100
  70.  
  71. local killEvent = game:GetService("ReplicatedStorage"):WaitForChild("BountyReplicatedStorage"):WaitForChild("KillEvent")
  72.  
  73. killEvent.Event:Connect(function(playerWhoKilled:Player, playerWhoDied:Player)
  74.    
  75.     playerWhoKilled.leaderstats.Bounty.Value += bountyPerKill
  76.     playerWhoKilled.leaderstats.Cash.Value += playerWhoDied.leaderstats.Bounty.Value
  77.    
  78.     playerWhoDied.leaderstats.Bounty.Value = 0
  79. end)
  80.  
  81. --[[
  82. Call the
  83.  
  84.     BindableEvent:Fire(playerWhoKilled, playerWhoDied)
  85.  
  86. method when you handle the death of a player, such as in the script for a sword.
  87. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement