Advertisement
Tweak16

ROBLOX Scripting | Skip Stage + Reset Stage + Saving Checkpoints

Apr 26th, 2021
5,725
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.53 KB | None | 0 0
  1. --[[
  2. Made by Tweakified, Neonblox Games
  3. Video Obby Checkpoints: https://youtu.be/WRXBa8AwoXo
  4. Video DataStore Addon: https://youtu.be/k_SGagxVOmc
  5. Video SkipStage Addon: https://youtu.be/Yki2Y-IuNnA
  6. Video ResetStage Addon: https://youtu.be/IkLQHJOKwDU
  7. Discord Support: https://discord.com/invite/JdaFf7p
  8.  
  9. Script Type: Regular Script
  10. Script Parent: ServerScriptService
  11. --]]
  12.  
  13. local Players = game:GetService("Players")
  14. local MarketplaceService = game:GetService("MarketplaceService")
  15. local DataStoreService = game:GetService("DataStoreService")
  16. local ObbyDataStore = DataStoreService:GetDataStore("ObbyDataStore")
  17. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  18.  
  19. local SkipStage = ReplicatedStorage:WaitForChild("SkipStage")
  20. local ResetStage = ReplicatedStorage:WaitForChild("ResetStage")
  21.  
  22. local Checkpoints = workspace:WaitForChild("Checkpoints")
  23. local inGameStartupPlayers = {}
  24. local CurrentStage = {}
  25. local TouchDb = {}
  26.  
  27. local ProductId = 1135733177 -- Change to your developer product id
  28.  
  29. local function NewCharacter(player, char)
  30.     local TempCurrentStage = CurrentStage[player.UserId]
  31.     if TempCurrentStage ~= nil then
  32.         local TempCheckpoint = Checkpoints:FindFirstChild(TempCurrentStage)
  33.         if TempCheckpoint ~= nil then
  34.             repeat wait(0.1) until char.PrimaryPart ~= nil
  35.             char:SetPrimaryPartCFrame(CFrame.new(TempCheckpoint.Position + Vector3.new(0, 3, 0)) * CFrame.Angles(0, math.rad(TempCheckpoint.Orientation.Y) + math.rad(90), 0))
  36.         end
  37.     end
  38. end
  39.  
  40. local function NewPlayer(player)
  41.     local success, stage = pcall(function()
  42.         return (ObbyDataStore:GetAsync(player.UserId)) or 1
  43.     end)
  44.  
  45.     CurrentStage[player.UserId] = stage
  46.  
  47.     local leaderstats = Instance.new("Folder", player)
  48.     leaderstats.Name = "leaderstats"
  49.     local Stage = Instance.new("IntValue", leaderstats)
  50.     Stage.Name = "Stage"
  51.     Stage.Value = stage
  52.  
  53.     local TempChar = player.Character
  54.     if TempChar ~= nil then
  55.         NewCharacter(player, TempChar)
  56.     end
  57.     player.CharacterAdded:Connect(function(char)
  58.         NewCharacter(player, char)
  59.     end)
  60. end
  61.  
  62. Players.PlayerAdded:Connect(function(player)
  63.     if inGameStartupPlayers[player] == nil then
  64.         NewPlayer(player)
  65.     end
  66. end)
  67.  
  68. Players.PlayerRemoving:Connect(function(player)
  69.     local success = pcall(function()
  70.         ObbyDataStore:SetAsync(player.UserId, CurrentStage[player.UserId])
  71.     end)
  72.     CurrentStage[player.UserId] = nil
  73. end)
  74.  
  75. SkipStage.OnServerInvoke = function(player)
  76.     local connection
  77.     local leaderstats = player:FindFirstChild("leaderstats")
  78.     if leaderstats ~= nil then
  79.         local Stage = leaderstats:FindFirstChild("Stage")
  80.         if Stage ~= nil then
  81.             if #Checkpoints:GetChildren() ~= Stage.Value then
  82.                 local PurchaseResult = "Purchase Failed"
  83.                 connection = MarketplaceService.PromptProductPurchaseFinished:Connect(function(userId, productId, purchased)
  84.                     if player.UserId == userId and productId == ProductId then
  85.                         if purchased == true then
  86.                             PurchaseResult = "Success"
  87.                         end
  88.                     end
  89.                     connection:Disconnect()
  90.                 end)
  91.                 MarketplaceService:PromptProductPurchase(player, ProductId)
  92.                 repeat wait(0.1) until connection.Connected == false or Players:GetPlayerByUserId(player.UserId) == nil
  93.                 return PurchaseResult
  94.             else
  95.                 return "You have reached the highest stage!"
  96.             end
  97.         end
  98.     end
  99. end
  100.  
  101. ResetStage.OnServerEvent:Connect(function(player)
  102.     CurrentStage[player.UserId] = 1
  103.     local TempLeaderstats = player:FindFirstChild("leaderstats")
  104.     if TempLeaderstats ~= nil then
  105.         local TempStage = TempLeaderstats:FindFirstChild("Stage")
  106.         if TempStage ~= nil then
  107.             TempStage.Value = 1
  108.         end
  109.     end    
  110.     NewCharacter(player, player.Character)
  111. end)
  112.  
  113. MarketplaceService.ProcessReceipt = function(recieptInfo)
  114.     if recieptInfo.ProductId == ProductId then
  115.         local player = Players:GetPlayerByUserId(recieptInfo.PlayerId)
  116.         if player ~= nil then
  117.             CurrentStage[player.UserId] = CurrentStage[player.UserId] + 1
  118.             local leaderstats = player:FindFirstChild("leaderstats")
  119.             if leaderstats ~= nil then
  120.                 local Stage = leaderstats:FindFirstChild("Stage")
  121.                 if Stage ~= nil then
  122.                     Stage.Value = CurrentStage[player.UserId]
  123.                 end
  124.             end
  125.             local TempChar = player.Character
  126.             if TempChar ~= nil then
  127.                 NewCharacter(player, TempChar)
  128.             end
  129.             return Enum.ProductPurchaseDecision.PurchaseGranted
  130.         end
  131.     end
  132.     return Enum.ProductPurchaseDecision.NotProcessedYet
  133. end
  134.  
  135. for i,v in pairs(Checkpoints:GetChildren()) do
  136.     local StageNum = tonumber(v.Name)
  137.     v.Touched:Connect(function(hit)
  138.         local char = hit.Parent
  139.         if char ~= nil then
  140.             local Humanoid = char:FindFirstChildOfClass("Humanoid")
  141.             if Humanoid ~= nil and Humanoid.Health > 0 then
  142.                 local player = Players:GetPlayerFromCharacter(char)
  143.                 if player ~= nil and (TouchDb[player.UserId] or 0) + 1 <= os.time() then
  144.                     TouchDb[player.UserId] = os.time()
  145.                     local TempCurrentStage = CurrentStage[player.UserId]
  146.                     if TempCurrentStage == StageNum - 1 then
  147.                         CurrentStage[player.UserId] = StageNum
  148.                         local TempLeaderstats = player:FindFirstChild("leaderstats")
  149.                         if TempLeaderstats ~= nil then
  150.                             local TempStage = TempLeaderstats:FindFirstChild("Stage")
  151.                             if TempStage ~= nil then
  152.                                 TempStage.Value = StageNum
  153.                             end
  154.                         end
  155.                     end
  156.                 end
  157.             end
  158.         end
  159.     end)
  160. end
  161.  
  162. inGameStartupPlayers = Players:GetPlayers()
  163. for i,v in pairs(inGameStartupPlayers) do
  164.     spawn(function()
  165.         NewPlayer(v)
  166.     end)
  167. end
  168.  
  169. game:BindToClose(function()
  170.     for i,v in pairs(Players:GetPlayers()) do
  171.         ObbyDataStore:SetAsync(v.UserId, CurrentStage[v.UserId])
  172.     end
  173.     wait(1)
  174. end)
  175.  
  176. inGameStartupPlayers = {}
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement