Advertisement
Cakey3101

Favourite Server

May 15th, 2025
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.92 KB | Source Code | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local RunService = game:GetService("RunService")
  5.  
  6. local DataStore = DataStoreService:GetDataStore("MyDataStore1-")
  7.  
  8. local Remotes = ReplicatedStorage.Remotes
  9.  
  10. local PlayerData = {}
  11.  
  12. local function PlayerAdded(Player: Player)
  13.     local Leaderstats = Instance.new("Folder", Player)
  14.     Leaderstats.Name = "leaderstats"
  15.    
  16.     local Gems = Instance.new("NumberValue", Leaderstats)
  17.     Gems.Name = "Gems"
  18.     Gems.Value = 0
  19.    
  20.     local UserId = Player.UserId
  21.     local Key = "Player_"..UserId
  22.    
  23.     local Success, SavedData = pcall(function()
  24.         return DataStore:GetAsync(Key)
  25.     end)
  26.    
  27.     if Success then
  28.         PlayerData[UserId] = {
  29.             HasFavourited = SavedData == true
  30.         }
  31.     else
  32.         warn("Failed To Load Data For Player")
  33.         PlayerData[UserId] = {
  34.             HasFavourited = false
  35.         }
  36.     end
  37. end
  38.  
  39. local function PlayerRemoving(Player: Player)
  40.     local UserId = Player.UserId
  41.     local Key = "Player_"..UserId
  42.     local SavedData = PlayerData[UserId]
  43.    
  44.     if SavedData then
  45.         pcall(function()
  46.             DataStore:SetAsync(Key, SavedData.HasFavourited)
  47.         end)
  48.     end
  49.    
  50.     PlayerData[UserId] = nil
  51. end
  52.  
  53. local function BindToClose()
  54.     for _, Player in Players:GetPlayers() do
  55.         PlayerRemoving(Player)
  56.     end
  57. end
  58.  
  59. local function OnRemote(Player: Player)
  60.     local UserId = Player.UserId
  61.     local SavedData = PlayerData[UserId]
  62.    
  63.     if not SavedData or SavedData.HasFavourited then
  64.         print("Already Claimed!")
  65.         return
  66.     end
  67.    
  68.     local Gems = Player:FindFirstChild("leaderstats") and Player.leaderstats:FindFirstChild("Gems")
  69.     if Gems then
  70.         Gems.Value += 10_000
  71.         SavedData.HasFavourited = true
  72.         print("Recieved Reward!")
  73.     end
  74. end
  75.  
  76. Players.PlayerAdded:Connect(PlayerAdded)
  77. Players.PlayerRemoving:Connect(PlayerRemoving)
  78. game:BindToClose(BindToClose)
  79. Remotes.GameFavourited.OnServerEvent:Connect(OnRemote)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement