Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. --Made By gangman67
  2. local DS = game:GetService("DataStoreService"):GetDataStore("DataTest")
  3. local WS = game:GetService("Workspace")
  4. local SS = game:GetService("ServerStorage")
  5. local RS = game:GetService("ReplicatedStorage")
  6. local Plrs = game:GetService("Players")
  7. local HS = game:GetService("HttpService")
  8. --These are Your Default Stats
  9. -- All data will be put into a folder found it replicatedStorage
  10. local DefaultStats ={
  11. "Level:1";
  12. "Exp:1";
  13. }
  14. local function isNumbers(input) -- Checks if the data is a number
  15. return (tonumber(input) and true or false) ii if so return true
  16. end
  17. function DataHandle(Data, Table) -- this splits "DataName:Value: and makes a value depending if number or string
  18. local FindData = ""
  19. local NewData = ""
  20. for tablen, Var in pairs(Table) do
  21. for i = 1, string.len(Var) do
  22. if Var:sub(i,i) == ":"then
  23. FindData = Var:sub(1,(i-1))
  24. NewData = Var:sub((i+1),string.len(Var))
  25. end
  26. end
  27. if isNumbers(NewData) == true then
  28. local val = Instance.new("NumberValue",Data)
  29. val.Name = FindData
  30. val.Value = NewData
  31. elseif isNumbers(NewData) == false then
  32. local val = Instance.new("StringValue",Data)
  33. val.Name = FindData
  34. val.Value = NewData
  35. end
  36. end
  37. end
  38. game.Players.PlayerRemoving:connect(function(Plr) --Saves plr data when they leave
  39. local key = "Data_"..Plr.UserId
  40. local DataTable ={}
  41. local data = RS:WaitForChild("Data_"..Plr.UserId)
  42. for DataN, Data in pairs(data:GetChildren()) do
  43. if Data:IsA("NumberValue") or Data:IsA("StringValue") then
  44. table.insert(DataTable, Data.Name..":"..Data.Value)
  45. end
  46. end
  47. local newData= HS:JSONEncode(DataTable)
  48. DS:SetAsync(key, newData)
  49. end)
  50. game.Players.PlayerAdded:connect(function(Plr) --Loads and configured data when they join
  51. local Key = "Data_"..Plr.UserId
  52. local DummyData = DS:GetAsync(Key)
  53. local DataF = Instance.new("Folder", RS)
  54. DataF.Name = "Data_"..Plr.UserId
  55. if DummyData ~= nil then
  56. local StoredData = HS:JSONDecode(DummyData)
  57. DataHandle(DataF, StoredData)
  58. else
  59. DataHandle(DataF, DefaultStats)
  60. end
  61. end)
  62. game.OnClose = function() -- keeps the server open 5 seconds after its shutdown
  63. for I,v in pairs(game.Players:GetChildren()) do -- this kicks the players which will fire the save function
  64. v:Kick("The Server Has shutdown we saved your data") --Msg the player when they leave
  65. wait(.1)
  66. end
  67. wait(5)
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement