Advertisement
Guest User

RBX Tool DataStore #3

a guest
Oct 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. --StarterGear Save Script
  2. -- jaredvaldez2/jaredvaldez4
  3. forceSaveCommand = "save/"
  4. forceLoadCommand = "load/"
  5. autoToolSaveTime = 120 -- How many seconds until autosave
  6. clearStarterGearOnLoad = true
  7. dataReadyWaitTimeout = 8 -- How many seconds will it wait for dataready.
  8. saveSuccessMessage = "Tools saved!" -- save text
  9. loadSuccessMessage = "Tools loaded!" -- load text
  10. secondsL = 120 -- Seconds to load
  11. secondsS = 30 -- Seconds to save
  12. loadErrorMessage = "Your tools could not be loaded. Chat \"" .. forceLoadCommand .. "\" to retry."
  13. saveErrorMessage = "Your tools could not be saved. Chat \"" .. forceSaveCommand .. "\" to retry."
  14. loadFastMessage = "You can only load your tools every 2 minutes."
  15. saveFastMessage = "You can only save your tools every 15 seconds."
  16. dpPrefix = "SavedTools-0-" -- Don't change, unless you want to wipe all data from tool saves.
  17. debug = false
  18. save_times = {}
  19. load_times = {}
  20. debris = game:GetService("Debris")
  21. function tellPlayer(player, text, time)
  22. if not player.Parent then return end
  23. local m = Instance.new("Message", player:findFirstChild("PlayerGui"))
  24. m.Text = text
  25. debris:AddItem(m, time or 3)
  26. end
  27. function waitForDataReady(player)
  28. local start = tick()
  29. while tick() < start + dataReadyWaitTimeout do
  30. if player.DataReady then return true end
  31. wait()
  32. end
  33. return false
  34. end
  35. function playerSave(player)
  36. if not waitForDataReady(player) then tellPlayer(player, saveErrorMessage) return end
  37. if (save_times[player] or 0) + secondsS > tick() then tellPlayer(player, saveFastMessage) return end
  38. save_times[player] = tick()
  39. local gear = player:findFirstChild("StarterGear")
  40. local tools = gear:GetChildren()
  41. local worked = false
  42. pcall(function ()
  43. player:SaveNumber(dpPrefix .. "Count", #tools)
  44. worked = true
  45. end)
  46. for i, tool in pairs(tools) do
  47. pcall(function ()
  48. player:SaveInstance(dpPrefix .. tostring(i), tool)
  49. end)
  50. end
  51. tellPlayer(player, saveSuccessMessage)
  52. end
  53. function playerLoad(player)
  54. if not waitForDataReady(player) then
  55. tellPlayer(player, loadErrorMessage)
  56. return
  57. end
  58. if (load_times[player] or 0) + secondsL > tick() then tellPlayer(player, loadFastMessage) return end
  59. load_times[player] = tick()
  60. local gear = player:findFirstChild("StarterGear")
  61. local backpack = player:findFirstChild("Backpack")
  62. local current = gear:GetChildren()
  63. local count = 0
  64. pcall(function ()
  65. count = player:LoadNumber(dpPrefix .. "Count")
  66. end)
  67. local tools = {}
  68. for i = 1, count do
  69. pcall(function ()
  70. tools[#tools + 1] = player:LoadInstance(dpPrefix .. tostring(i))
  71. end)
  72. end
  73. for i, tool in pairs(tools) do
  74. local copy = tool:clone()
  75. copy.Parent = backpack
  76. local copy2 = tool:clone()
  77. copy2.Parent = gear
  78. end
  79. if clearStarterGearOnLoad then
  80. for i, tool in pairs(current) do
  81. tool:remove()
  82. end
  83. end
  84. tellPlayer(player, loadSuccessMessage)
  85. end
  86. function onPlayerChatted(player, message, recipient)
  87. message = message:lower()
  88. if message == forceSaveCommand then
  89. playerSave(player)
  90. elseif message == forceLoadCommand then
  91. local s, e = pcall(function ()
  92. playerLoad(player)
  93. end)
  94. if not s then
  95. if debug then tellPlayer(player, "playerLoad error:" .. e) end
  96. end
  97. end
  98. end
  99. function onPlayerEntered(player)
  100. player.Chatted:connect(function (msg, rec) onPlayerChatted(player, msg, rec) end)
  101. playerLoad(player)
  102. coroutine.wrap(function()
  103. while wait(autoToolSaveTime) do
  104. playerSave(player)
  105. end
  106. end)()
  107. end
  108. function onPlayerLeft(player)
  109. local s, e = pcall(function ()
  110. playerSave(player)
  111. end)
  112. if not s then
  113. if debug then Instance.new("Message", workspace).Text = "playerSave error:" .. e end
  114. end
  115. end
  116. game.Players.PlayerAdded:connect(onPlayerEntered)
  117. game.Players.PlayerRemoving:connect(onPlayerLeft)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement