Advertisement
SxScripting

Shop System Server Script

Jul 10th, 2021
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. local RS = game:GetService("ReplicatedStorage")
  2. local DSS = game:GetService("DataStoreService")
  3.  
  4. local Data = DSS:GetDataStore("Save1")
  5.  
  6. local SHOPCOSTS = {
  7. HealthPotion = 100;
  8. SpeedPotion = 200;
  9. SuperHealthPotion = 500;
  10. SuperSpeedPotion = 1000;
  11. }
  12.  
  13. local Stats = {}
  14. local Deb = {}
  15.  
  16. local function ChangeMoney(Player, Money)
  17. Stats[Player].Money = Money
  18. Player.leaderstats.Money.Value = tostring(Money)
  19. end
  20.  
  21. local function playerJoined(Player)
  22. Stats[Player] = {}
  23. Deb[Player] = {}
  24. local leaderstats = Instance.new("Folder", Player)
  25. leaderstats.Name = "leaderstats"
  26.  
  27. local Money = Instance.new("StringValue", leaderstats)
  28. Money.Name = "Money"
  29.  
  30. local success, Save = pcall(function()
  31. return Data:GetAsync(Player.UserId)
  32. end)
  33.  
  34. if success then
  35. if Save then
  36. ChangeMoney(Player, Save.Money)
  37. else
  38. ChangeMoney(Player, 10000)
  39. end
  40. else
  41. Player:Kick("Failed to load data. Please Rejoin")
  42. end
  43. end
  44.  
  45. local function playerRemoving(Player)
  46. if Stats[Player] and Stats[Player].Loaded then
  47. Data:SetAsync(Player.UserId, Stats[Player])
  48. end
  49. Stats[Player] = nil
  50. Deb[Player] = nil
  51. end
  52.  
  53. local function Heals(Player, Heal)
  54. coroutine.wrap(function()
  55. Deb[Player].Health = true
  56. local Loop = 0
  57. while wait(1) and Loop <= 10 do
  58. Player.Character.Humanoid.Health += Heal
  59. Loop = Loop + 1
  60. end
  61. Deb[Player].Health = nil
  62. end)()
  63. end
  64.  
  65. local function Speeds(Player, Speed)
  66. coroutine.wrap(function()
  67. Deb[Player].Speed = true
  68. Player.Character.Humanoid.WalkSpeed = Speed
  69. delay(10, function()
  70. Player.Character.Humanoid.WalkSpeed = 16
  71. Deb[Player].Speed = nil
  72. end)
  73. end)()
  74. end
  75.  
  76. local function potionActivated(Player, Potion)
  77. local PotionInstance = Player.Character:FindFirstChild(Potion) or Player.Backpack:FindFirstChild(Potion)
  78. if not PotionInstance then return end
  79. if Potion == "HealthPotion" then
  80. if Deb[Player].Health then return end
  81. local HEAL_PER_SECOND = 2
  82. Heals(Player, HEAL_PER_SECOND)
  83. elseif Potion == "SuperHealthPotion" then
  84. if Deb[Player].Health then return end
  85. local HEAL_PER_SECOND = 5
  86. Heals(Player, HEAL_PER_SECOND)
  87. elseif Potion == "SpeedPotion" then
  88. if Deb[Player].Speed then return end
  89. local SPEED = 20
  90. Speeds(Player, SPEED)
  91. elseif Potion == "SuperSpeedPotion" then
  92. if Deb[Player].Speed then return end
  93. local SPEED = 26
  94. Speeds(Player, SPEED)
  95. end
  96. PotionInstance:Destroy()
  97. end
  98.  
  99. local function shopItemBuyer(Player, Item)
  100. local COST = SHOPCOSTS[Item]
  101. local ITEMINSTANCE = RS.ShopItems:FindFirstChild(Item)
  102. if not COST or not ITEMINSTANCE then return end
  103. if Stats[Player].Money < COST then return end
  104. ChangeMoney(Player, Stats[Player].Money - COST)
  105. local Clone = ITEMINSTANCE:Clone()
  106. Clone.Parent = Player.Backpack
  107. end
  108.  
  109. game.Players.PlayerAdded:Connect(playerJoined)
  110. game.Players.PlayerRemoving:Connect(playerRemoving)
  111. RS.Events.potionActivation.OnServerEvent:Connect(potionActivated)
  112. RS.Events.buyShopItem.OnServerEvent:Connect(shopItemBuyer)
  113.  
  114. game:BindToClose(function()
  115. if game["Run Service"]:IsStudio() then
  116. return
  117. end
  118. for i,v in pairs(game.Players:GetPlayers()) do
  119. playerRemoving(v)
  120. end
  121. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement