Davk0

NoDataSave

Sep 14th, 2019
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.10 KB | None | 0 0
  1. -- Stats / Attributes
  2. lvl=game:GetService("DataStoreService"):GetOrderedDataStore("Lvl")
  3. xp=game:GetService("DataStoreService"):GetOrderedDataStore("XP")
  4. gp=game:GetService("DataStoreService"):GetOrderedDataStore("Gold")
  5.  
  6. str=game:GetService("DataStoreService"):GetOrderedDataStore("Strength")
  7. con=game:GetService("DataStoreService"):GetOrderedDataStore("Constitution")
  8. dex=game:GetService("DataStoreService"):GetOrderedDataStore("Dexterity")
  9. int=game:GetService("DataStoreService"):GetOrderedDataStore("Intelligence")
  10.  
  11. -- Armors
  12. leather=game:GetService("DataStoreService"):GetDataStore("UnlockedLeather")
  13. chainmail=game:GetService("DataStoreService"):GetDataStore("UnlockedChainmail")
  14. iron=game:GetService("DataStoreService"):GetDataStore("UnlockedIron")
  15. steel=game:GetService("DataStoreService"):GetDataStore("UnlockedSteel")
  16. mithril=game:GetService("DataStoreService"):GetDataStore("UnlockedMithril")
  17. demonic=game:GetService("DataStoreService"):GetDataStore("UnlockedDemonic")
  18. bone=game:GetService("DataStoreService"):GetDataStore("UnlockedBone")
  19. bronze=game:GetService("DataStoreService"):GetDataStore("UnlockedBronze")
  20. adamant=game:GetService("DataStoreService"):GetDataStore("UnlockedAdamant")
  21. equipped=game:GetService("DataStoreService"):GetDataStore("EquippedArmor")
  22.  
  23. -- PvP
  24. pvp=game:GetService("DataStoreService"):GetOrderedDataStore("PvP Points")
  25.  
  26.  
  27. local Properties = game.Workspace.GAME_PROPERTIES
  28.  
  29. function onXPChanged(player, XP, level)
  30. if string.lower(Properties.XPType.Value) == "linear" then --Linear (1, 2, 3, 4, 5, etc)(High level cap recommended.)
  31. if XP.Value>= Properties.XPPerLevel.Value * (level.Value) + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then
  32. XP.Value = XP.Value - (Properties.XPPerLevel.Value*(level.Value) + Properties.AdditionalXPNeeded.Value)
  33. level.Value = level.Value + 1
  34. end
  35. elseif string.lower(Properties.XPType.Value) == "square" then --Square (1, 4, 9, 16, 25, etc)(Medium level cap recommended.)
  36. if XP.Value>= Properties.XPPerLevel.Value * (level.Value * level.Value) + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then
  37. XP.Value = XP.Value - (Properties.XPPerLevel.Value * (level.Value * level.Value) + Properties.AdditionalXPNeeded.Value)
  38. level.Value = level.Value + 1
  39. end
  40. elseif string.lower(Properties.XPType.Value) == "exponent" then --Exponent (1, 2, 4, 8, 16, etc)(Low level cap recommended. Exponent rate dictated by XPPerLevel divided by 100.)
  41. if XP.Value>= math.pow(Properties.XPExponentMagnitude.Value/100, level.Value-1) * Properties.XPPerLevel.Value + Properties.AdditionalXPNeeded.Value and level.Value < Properties.LevelCap.Value then
  42. XP.Value = XP.Value - math.pow(Properties.XPExponentMagnitude.Value/100, level.Value-1) * Properties.XPPerLevel.Value + Properties.AdditionalXPNeeded.Value
  43. level.Value = level.Value + 1
  44. end
  45. end
  46. end
  47.  
  48. function onLevelUp(player, XP, level)
  49. if player.Character~=nil then
  50. for i = 1,5 do
  51. local fireworks = Instance.new("Part")
  52. fireworks.Shape = 0
  53. fireworks.formFactor = "Symmetric"
  54. fireworks.Size = Vector3.new(1,1,1)
  55. fireworks.BrickColor = BrickColor.Random()
  56. fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0)
  57. fireworks.Parent = game.Workspace
  58. game:GetService("Debris"):AddItem(fireworks, 2)
  59. fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30))
  60. end
  61. end
  62. local m = Instance.new("Hint")
  63. m.Parent = game.Workspace
  64. m.Text = player.Name .. " has gained a level! He/she is now level " .. level.Value .. "!"
  65. wait(5)
  66. m.Parent = nil
  67. end
  68.  
  69.  
  70. function saveScores(player)
  71. lvl:SetAsync(player.userId, player.leaderstats.Lvl.Value)
  72. xp:SetAsync(player.userId, player.leaderstats.XP.Value)
  73. gp:SetAsync(player.userId, player.leaderstats.Gold.Value)
  74. print("Stats saved!")
  75. end
  76.  
  77. function saveScorePvP(player)
  78. pvp:SetAsync(player.userId, player.leaderstats:FindFirstChild("PvP Points").Value)
  79. end
  80.  
  81. function saveAttributes(player)
  82. str:SetAsync(player.userId, player.attributes.Strength.Value)
  83. con:SetAsync(player.userId, player.attributes.Constitution.Value)
  84. dex:SetAsync(player.userId, player.attributes.Dexterity.Value)
  85. int:SetAsync(player.userId, player.attributes.Intelligence.Value)
  86. print("Attributes saved!")
  87. end
  88.  
  89. function saveArmor(player)
  90. leather:SetAsync(player.userId, player.UnlockedArmor.UnlockedLeather.Value)
  91. chainmail:SetAsync(player.userId, player.UnlockedArmor.UnlockedChainmail.Value)
  92. iron:SetAsync(player.userId, player.UnlockedArmor.UnlockedIron.Value)
  93. steel:SetAsync(player.userId, player.UnlockedArmor.UnlockedSteel.Value)
  94. mithril:SetAsync(player.userId, player.UnlockedArmor.UnlockedMithril.Value)
  95. demonic:SetAsync(player.userId, player.UnlockedArmor.UnlockedDemonic.Value)
  96. bone:SetAsync(player.userId, player.UnlockedArmor.UnlockedBone.Value)
  97. bronze:SetAsync(player.userId, player.UnlockedArmor.UnlockedBronze.Value)
  98. adamant:SetAsync(player.userId, player.UnlockedArmor.UnlockedAdamant.Value)
  99. equipped:SetAsync(player.userId, player.stats.EquippedArmor.Value)
  100. print("Armor saved successfully!")
  101. end
  102.  
  103.  
  104. function loadScores(player)
  105. player.leaderstats.Lvl.Value = lvl:GetAsync(player.userId)
  106. player.leaderstats.XP.Value = xp:GetAsync(player.userId)
  107. player.leaderstats.Gold.Value = gp:GetAsync(player.userId)
  108. if (player.leaderstats.Lvl.Value == 0) then
  109. player.leaderstats.Lvl.Value = 1 -- Makes sure always level 1.
  110. end
  111. end
  112.  
  113. function loadScorePvP(player)
  114. player.leaderstats:FindFirstChild("PvP Points").Value = pvp:GetAsync(player.userId)
  115. end
  116.  
  117. function loadAttributes(player)
  118. local attrb1 = str:getAsync(player.userId)
  119. local attrb2 = con:getAsync(player.userId)
  120. local attrb3 = int:getAsync(player.userId)
  121. local attrb4 = dex:getAsync(player.userId)
  122.  
  123. if attrb1 >= Properties.StartingAttributes.Value then
  124. player.attributes.Strength.Value = attrb1
  125. else
  126. player.attributes.Strength.Value = Properties.StartingAttributes.Value
  127. end
  128.  
  129. if attrb2 >= Properties.StartingAttributes.Value then
  130. player.attributes.Constitution.Value = attrb2
  131. else
  132. player.attributes.Constitution.Value = Properties.StartingAttributes.Value
  133. end
  134.  
  135. if attrb3 >= Properties.StartingAttributes.Value then
  136. player.attributes.Intelligence.Value = attrb3
  137. else
  138. player.attributes.Intelligence.Value = Properties.StartingAttributes.Value
  139. end
  140.  
  141. if attrb4 >= Properties.StartingAttributes.Value then
  142. player.attributes.Dexterity.Value = attrb4
  143. else
  144. player.attributes.Dexterity.Value = Properties.StartingAttributes.Value
  145. end
  146. end
  147.  
  148. function loadArmors(player)
  149. local armor1 = leather:getAsync(player.userId)
  150. local armor2 = chainmail:getAsync(player.userId)
  151. local armor3 = iron:getAsync(player.userId)
  152. local armor4 = steel:getAsync(player.userId)
  153. local armor5 = mithril:getAsync(player.userId)
  154. local armor6 = demonic:getAsync(player.userId)
  155. local armor7 = bone:getAsync(player.userId)
  156. local armor8 = bronze:getAsync(player.userId)
  157. local armor9 = adamant:getAsync(player.userId)
  158. local armorEquipped = equipped:getAsync(player.userId)
  159.  
  160. if armor1 == true then
  161. player.UnlockedArmor.UnlockedLeather.Value = armor1
  162. end
  163. if armor2 == true then
  164. player.UnlockedArmor.UnlockedChainmail.Value = armor2
  165. end
  166. if armor3 == true then
  167. player.UnlockedArmor.UnlockedIron.Value = armor3
  168. end
  169. if armor4 == true then
  170. player.UnlockedArmor.UnlockedSteel.Value = armor4
  171. end
  172. if armor5 == true then
  173. player.UnlockedArmor.UnlockedMithril.Value = armor5
  174. end
  175. if armor6 == true then
  176. player.UnlockedArmor.UnlockedDemonic.Value = armor6
  177. end
  178. if armor7 == true then
  179. player.UnlockedArmor.UnlockedBone.Value = armor7
  180. end
  181. if armor8 == true then
  182. player.UnlockedArmor.UnlockedBronze.Value = armor8
  183. end
  184. if armor9 == true then
  185. player.UnlockedArmor.UnlockedAdamant.Value = armor9
  186. end
  187. if armorEquipped ~= nil then
  188. player.Stats.EquippedArmor.Value = armorEquipped
  189. end
  190. end
  191.  
  192. function onPlayerEntered(newPlayer)
  193.  
  194. --Variables folders
  195. local stats = Instance.new("Model")
  196. stats.Name = "leaderstats"
  197.  
  198. local attrb = Instance.new("Model")
  199. attrb.Name = "attributes"
  200.  
  201. local misc = Instance.new("Model")
  202. misc.Name = "Stats"
  203.  
  204. local armor = Instance.new("Model")
  205. armor.Name = "UnlockedArmor"
  206.  
  207. --Leaderstats variables
  208. local clicks = Instance.new("IntValue", stats)
  209. clicks.Name = "Lvl"
  210. clicks.Value = 1
  211.  
  212. local clicks2 = Instance.new("IntValue", stats)
  213. clicks2.Name = "XP"
  214. clicks2.Value = 0
  215.  
  216. local clicks3 = Instance.new("IntValue", stats)
  217. clicks3.Name = "Gold"
  218. clicks3.Value = 0
  219.  
  220. --Attributes
  221. local attrb1 = Instance.new("IntValue", attrb)
  222. attrb1.Name = "Strength"
  223. attrb1.Value = Properties.StartingAttributes.Value
  224.  
  225. local attrb2 = Instance.new("IntValue", attrb)
  226. attrb2.Name = "Constitution"
  227. attrb2.Value = Properties.StartingAttributes.Value
  228.  
  229. local attrb3 = Instance.new("IntValue", attrb)
  230. attrb3.Name = "Intelligence"
  231. attrb3.Value = Properties.StartingAttributes.Value
  232.  
  233. local attrb4 = Instance.new("IntValue", attrb)
  234. attrb4.Name = "Dexterity"
  235. attrb4.Value = Properties.StartingAttributes.Value
  236.  
  237. --Other stats
  238. local mana = Instance.new("IntValue", misc)
  239. mana.Name = "Mana"
  240. mana.Value = 100
  241.  
  242. local maxmana = Instance.new("IntValue", misc)
  243. maxmana.Name = "MaxMana"
  244. maxmana.Value = 100
  245.  
  246. local equippedArmor = Instance.new("StringValue", misc)
  247. equippedArmor.Name = "EquippedArmor"
  248. equippedArmor.Value = "None"
  249.  
  250. local defense = Instance.new("IntValue", misc)
  251. defense.Name = "Defense"
  252. defense.Value = 0
  253.  
  254. --Unlocked armors
  255. local unlockedArmor1 = Instance.new("BoolValue", armor)
  256. unlockedArmor1.Name = "UnlockedLeather"
  257. unlockedArmor1.Value = false
  258.  
  259. local unlockedArmor2 = Instance.new("BoolValue", armor)
  260. unlockedArmor2.Name = "UnlockedChainmail"
  261. unlockedArmor2.Value = false
  262.  
  263. local unlockedArmor3 = Instance.new("BoolValue", armor)
  264. unlockedArmor3.Name = "UnlockedIron"
  265. unlockedArmor3.Value = false
  266.  
  267. local unlockedArmor4 = Instance.new("BoolValue", armor)
  268. unlockedArmor4.Name = "UnlockedSteel"
  269. unlockedArmor4.Value = false
  270.  
  271. local unlockedArmor5 = Instance.new("BoolValue", armor)
  272. unlockedArmor5.Name = "UnlockedMithril"
  273. unlockedArmor5.Value = false
  274.  
  275. local unlockedArmor6 = Instance.new("BoolValue", armor)
  276. unlockedArmor6.Name = "UnlockedDemonic"
  277. unlockedArmor6.Value = false
  278.  
  279. local unlockedArmor7 = Instance.new("BoolValue", armor)
  280. unlockedArmor7.Name = "UnlockedBone"
  281. unlockedArmor7.Value = false
  282.  
  283. local unlockedArmor8 = Instance.new("BoolValue", armor)
  284. unlockedArmor8.Name = "UnlockedBronze"
  285. unlockedArmor8.Value = false
  286.  
  287. local unlockedArmor9 = Instance.new("BoolValue", armor)
  288. unlockedArmor9.Name = "UnlockedAdamant"
  289. unlockedArmor9.Value = false
  290.  
  291. --PvP Points
  292. if Properties.HasPvP.Value == true then
  293. local clicksPvP = Instance.new("IntValue", stats)
  294. clicksPvP.Name = "PvP Points"
  295. clicksPvP.Value = 0
  296. end
  297.  
  298. --Assign the folders to player.
  299. stats.Parent = newPlayer
  300. attrb.Parent = newPlayer
  301. misc.Parent = newPlayer
  302. armor.Parent = newPlayer
  303.  
  304. clicks3.Changed:connect(function() saveScores(newPlayer) end)
  305. clicks2.Changed:connect(function() onXPChanged(newPlayer, clicks2, clicks) end)
  306. clicks.Changed:connect(function() onLevelUp(newPlayer, clicks2, clicks) saveScores(newPlayer) saveAttributes(newPlayer) saveArmor(newPlayer) end)
  307.  
  308. newPlayer:WaitForDataReady() -- IMPORTANT: after player.DataReady = true, it won't become false again. You need DataReady to be true for a player before you can load or save data.
  309.  
  310. loadAttributes(newPlayer)
  311. loadScores(newPlayer)
  312. loadArmors(newPlayer)
  313.  
  314. if Properties.HasPvP.Value == true then
  315. loadScorePvP(newPlayer)
  316. end
  317.  
  318. newPlayer.CharacterAdded:connect(function(c)
  319. c.Humanoid.MaxHealth = (game.Workspace.GAME_PROPERTIES.StartingHP.Value) + (game.Workspace.GAME_PROPERTIES.HPPerLevel.Value * newPlayer.leaderstats.Lvl.Value) + (Game.Workspace.GAME_PROPERTIES.HPPerConstitution.Value * newPlayer.attributes.Constitution.Value)
  320. c.Humanoid.Health = c.Humanoid.MaxHealth
  321. end)
  322. end
  323.  
  324. function onPlayerRemoving(player)
  325. print("Attempting to save score for " .. player.Name)
  326. local stats = player:FindFirstChild("leaderstats")
  327. local attrb = player:FindFirstChild("attributes")
  328. local misc = player:FindFirstChild("Stats")
  329. local armor = player:FindFirstChild("UnlockedArmor")
  330. --Save core stats.
  331. if (stats ~= nil) then
  332. local level = stats:FindFirstChild("Lvl")
  333. local experience = stats:FindFirstChild("XP")
  334. local gold = stats:FindFirstChild("Gold")
  335.  
  336. if (level ~= nil)and(experience ~= nil)and(gold ~= nil) then
  337. saveScores(player)
  338. end
  339.  
  340. local pointsPvP = stats:FindFirstChild("PvP Points")
  341. if (pointsPvP ~= nil) then
  342. saveScorePvP(player)
  343. end
  344. end
  345. --Save attributes.
  346. if (attrb ~= nil) then
  347. local attrb1 = attrb:FindFirstChild("Strength")
  348. local attrb2 = attrb:FindFirstChild("Constitution")
  349. local attrb3 = attrb:FindFirstChild("Intelligence")
  350. local attrb4 = attrb:FindFirstChild("Dexterity")
  351.  
  352. if (attrb1 ~= nil)and(attrb2 ~= nil)and(attrb3 ~= nil)and(attrb4 ~= nil) then
  353. saveAttributes(player)
  354. end
  355. end
  356. --Save unlocked armor.
  357. if (armor ~= nil) then
  358. local armor1 = armor:FindFirstChild("UnlockedLeather")
  359. local armor2 = armor:FindFirstChild("UnlockedChainmail")
  360. local armor3 = armor:FindFirstChild("UnlockedIron")
  361. local armor4 = armor:FindFirstChild("UnlockedSteel")
  362. local armor5 = armor:FindFirstChild("UnlockedMithril")
  363. local armor6 = armor:FindFirstChild("UnlockedDemonic")
  364. local armor7 = armor:FindFirstChild("UnlockedBone")
  365. local armor8 = armor:FindFirstChild("UnlockedBronze")
  366. local armor9 = armor:FindFirstChild("UnlockedAdamant")
  367. local armorEquipped = misc:FindFirstChild("EquippedArmor")
  368.  
  369. if (armor1 ~= nil)and(armor2 ~= nil)and(armor3 ~= nil)and(armor4 ~= nil)and(armor5 ~= nil)and(armor6 ~= nil)and(armor7 ~= nil)and(armor8 ~= nil)and(armor9 ~= nil)and(armorEquipped ~= nil) then
  370. saveArmor(player)
  371. end
  372. end
  373. end
  374.  
  375. game.Players.PlayerAdded:connect(onPlayerEntered)
  376. game.Players.PlayerRemoving:connect(onPlayerRemoving)
Advertisement
Add Comment
Please, Sign In to add comment