Advertisement
Sungmingamerpro13

Role leaderstats (Lobby and Main)

Jan 29th, 2025 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 1.18 KB | None | 0 0
  1. local TeleportService = game:GetService("TeleportService")
  2. local SaveRoles = game:GetService("DataStoreService"):GetDataStore("Role")
  3.  
  4. game.Players.PlayerAdded:Connect(function(player)
  5.  
  6.     local PreviousData = SaveRoles:GetAsync(player.UserId)
  7.     local Role
  8.  
  9.     if PreviousData ~= nil then
  10.         Role = PreviousData
  11.     else
  12.         Role = script.Role.Value
  13.         SaveRoles:SetAsync(player.UserId, Role)
  14.     end
  15.  
  16.     local RoleValue = Instance.new("StringValue", player)
  17.     RoleValue.Name = "Role"
  18.     RoleValue.Value = Role
  19.  
  20.     player:WaitForChild("Role"):GetPropertyChangedSignal("Value"):Connect(function()
  21.         player.Role.Value = player.Role.Value
  22.     end)
  23.  
  24. end)
  25.  
  26. game:BindToClose(function()
  27.     for i,player in pairs(game.Players:GetPlayers()) do
  28.         local value = player.Role.Value
  29.         SaveRoles:SetAsync(player.UserId, value)
  30.         print("Saved data for "..player.Name)
  31.     end
  32. end)
  33.  
  34. game.Players.PlayerRemoving:Connect(function(player)
  35.     local value = player.Role.Value
  36.  
  37.     if value ~= nil then
  38.         print("Found data to save for "..player.Name.."!")
  39.         SaveRoles:SetAsync(player.UserId, value)
  40.         print("Saved data for "..player.Name)
  41.     else
  42.         print("Did not manage to find data to save for "..player.Name.."!")
  43.     end
  44. end)
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement