Advertisement
OnFireRobloxScriptin

Tool Datastore

Jul 7th, 2024 (edited)
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.22 KB | None | 0 0
  1. --//Datastore
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local dataStore = DataStoreService:GetDataStore("stats")
  4.  
  5. --//Tools
  6. local rs = game.ReplicatedStorage
  7. local tools = rs:WaitForChild("Tools")
  8. local swordtool = tools:WaitForChild("ClassicSword")
  9. local flashlighttool = tools:WaitForChild("FlashLight")
  10. local keycardtool = tools:WaitForChild("SCP Card Level 4")
  11.  
  12. --//save data
  13. local function saveData(player) --Function that saves data
  14.     local tableToSave = { --Creates a table to save values
  15.         player.leaderstats.Cash.Value;
  16.         player.Tools["Classic Sword"].Value;
  17.         player.Tools["Flash Light"].Value;
  18.         player.Tools["Keycard"].Value;
  19.     }
  20.    
  21.     local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave) --SetAsync saves the data
  22.    
  23.     if success then
  24.         print("Data has been saved")
  25.     else
  26.         print("Data has not been saved!")
  27.     end
  28. end
  29.  
  30. --//leaderstats
  31. game.Players.PlayerAdded:Connect(function(player) --When player joins the game
  32.     player.CharacterAdded:Wait()
  33.     local leaderstats = Instance.new("Folder", player) --Creates new folder and puts it under the player
  34.     leaderstats.Name = "leaderstats" --Names the folder "leaderstats"
  35.    
  36.     local cash = Instance.new("IntValue", leaderstats) --Creates new Intvalue and puts it under the leaderstats folder
  37.     cash.Name = "Cash" --Names the IntValue "Cash" <- You can change Cash to any currency you like
  38.     cash.Value = 1600 --Gives the player 100 starting "Cash"
  39.    
  40.     local toolfolder = Instance.new("Folder", player) --Creates new folder for tools, puts it under the player
  41.     toolfolder.Name = "Tools"
  42.    
  43.     local sword = Instance.new("BoolValue", toolfolder) --Creates new Bookvalue and puts it under the toolfolder
  44.     sword.Name = "Classic Sword" --Names the boolvalue (Make sure this is the same as your table in ToolHandler)
  45.     sword.Value = false --Sets Boolvalue to be false
  46.    
  47.     local flashlight = Instance.new("BoolValue", toolfolder)
  48.     flashlight.Name = "Flash Light"
  49.     flashlight.Value = false
  50.    
  51.     local keycard = Instance.new("BoolValue", toolfolder)
  52.     keycard.Name = "Keycard"
  53.     keycard.Value = false
  54.    
  55.     local data = nil
  56.    
  57.     local success, errorMessage = pcall(function()
  58.         data = dataStore:GetAsync(player.UserId)
  59.     end)
  60.    
  61.     if success and data then --Get Async retrieves data from the data store, and loads them from the table
  62.         cash.Value = data[1]
  63.         sword.Value = data[2]
  64.         flashlight.Value = data[3]
  65.         keycard.Value = data[4]
  66.     else
  67.         print("The Player has no data!")
  68.         warn(errorMessage)
  69.     end
  70.    
  71.     if sword.Value == true then --This if statements check for the bookvalues and gives tools to players
  72.         local newtool = swordtool:Clone()
  73.         newtool.Parent = player.Backpack
  74.     end
  75.    
  76.     if flashlight.Value == true then
  77.         local newtool = flashlighttool:Clone()
  78.         newtool.Parent = player.Backpack
  79.     end
  80.    
  81.     if keycard.Value == true then
  82.         local newtool = keycardtool:Clone()
  83.         newtool.Parent = player.Backpack
  84.     end
  85.    
  86. end)
  87.  
  88.  
  89. game.Players.PlayerRemoving:Connect(function(player) --When player leaves the game, save data
  90.     saveData(player)
  91. end)
  92.  
  93. game:BindToClose(function() --When the game's server shuts down, save all player's data
  94.     for _, player in ipairs(game.Players:GetPlayers()) do
  95.         task.spawn(saveData, player)
  96.     end
  97. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement