Advertisement
HowToRoblox

MiningSimulatorServer

Nov 24th, 2022
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.44 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("Mining Simulator DataStore")
  3.  
  4. local rs = game.ReplicatedStorage:WaitForChild("MiningSimulatorReplicatedStorage")
  5. local re = rs:WaitForChild("RemoteEvent")
  6.  
  7. local stones = rs:WaitForChild("StoneTypes")
  8. local ores = rs:WaitForChild("OreTypes"):GetChildren()
  9. for i, stone in pairs(stones:GetChildren()) do
  10.     table.insert(ores, stone)
  11. end
  12.  
  13. local pickaxes = rs:WaitForChild("Pickaxes")
  14. local backpacks = rs:WaitForChild("Backpacks")
  15.  
  16. local moduleScripts = rs:WaitForChild("CONFIGURATION")
  17. local stonesConfig = require(moduleScripts:WaitForChild("Stones"))
  18. local oresConfig = require(moduleScripts:WaitForChild("Ores"))
  19. local pickaxesConfig = require(moduleScripts:WaitForChild("Pickaxes"))
  20. local backpacksConfig = require(moduleScripts:WaitForChild("Backpacks"))
  21. local otherConfig = require(moduleScripts:WaitForChild("OtherSettings"))
  22.  
  23. local animations = rs:WaitForChild("Animations")
  24.  
  25. local areas = workspace:WaitForChild("MiningSimulatorAreas")
  26.  
  27.  
  28. --Data handling
  29. function saveData(plr)
  30.  
  31.     if not plr:FindFirstChild("FAILED TO LOAD DATA") then
  32.  
  33.         local cash = plr.leaderstats.Cash.Value
  34.        
  35.         local pickaxe = plr.Equipment.Pickaxe.Value
  36.         local backpack = plr.Equipment.Backpack.Value
  37.        
  38.         local backpackContents = {}
  39.         for i, oreValue in pairs(plr.BackpackContents:GetChildren()) do
  40.             backpackContents[oreValue.Name] = oreValue.Value
  41.         end
  42.        
  43.         local compiledData = {
  44.             Cash = cash,
  45.             Pickaxe = pickaxe,
  46.             Backpack = backpack,
  47.             BackpackContents = backpackContents
  48.         }
  49.        
  50.  
  51.         local success, err
  52.         while not success do
  53.            
  54.             if err then
  55.                 warn(err)
  56.             end
  57.  
  58.             success, err = pcall(function()
  59.                 ds:SetAsync(plr.UserId, compiledData)
  60.             end)
  61.             task.wait(1)
  62.         end
  63.     end
  64. end
  65.  
  66. game.Players.PlayerRemoving:Connect(saveData)
  67.  
  68. game:BindToClose(function()
  69.     for i, plr in pairs(game.Players:GetPlayers()) do
  70.         saveData(plr)
  71.     end
  72. end)
  73.  
  74.  
  75. game.Players.PlayerAdded:Connect(function(plr)
  76.  
  77.     local ls = Instance.new("Folder")
  78.     ls.Name = "leaderstats"
  79.     ls.Parent = plr
  80.  
  81.     local cashValue = Instance.new("IntValue")
  82.     cashValue.Name = "Cash"
  83.     cashValue.Parent = ls
  84.    
  85.     local equipmentFolder = Instance.new("Folder")
  86.     equipmentFolder = Instance.new("Folder")
  87.     equipmentFolder.Name = "Equipment"
  88.     equipmentFolder.Parent = plr
  89.    
  90.     local pickaxeValue = Instance.new("StringValue")
  91.     pickaxeValue.Name = "Pickaxe"
  92.     pickaxeValue.Parent = equipmentFolder
  93.    
  94.     local backpackValue = Instance.new("StringValue")
  95.     backpackValue.Name = "Backpack"
  96.     backpackValue.Parent = equipmentFolder
  97.    
  98.     local backpackContents = Instance.new("Folder")
  99.     backpackContents.Name = "BackpackContents"
  100.     backpackContents.Parent = plr
  101.  
  102.     for i, ore in pairs(ores) do
  103.         local oreValue = Instance.new("IntValue")
  104.         oreValue.Name = ore.Name
  105.         oreValue.Parent = backpackContents
  106.     end
  107.    
  108.    
  109.     plr.CharacterAdded:Connect(function(char)
  110.        
  111.         repeat task.wait(0.1) until pickaxeValue.Value ~= nil and backpackValue.Value ~= nil
  112.  
  113.         local plrPickaxe = pickaxes[pickaxeValue.Value]:Clone()
  114.         plrPickaxe.Parent = plr:WaitForChild("Backpack")
  115.  
  116.         local plrBackpack = backpacks[backpackValue.Value]:Clone()
  117.         plrBackpack.Name = "BACKPACK"
  118.        
  119.         for i, descendant in pairs(plrBackpack:GetDescendants()) do
  120.             if descendant:IsA("BasePart") and descendant ~= plrBackpack.RootAttachment then
  121.                 local newWC = Instance.new("WeldConstraint")
  122.                 newWC.Part0 = plrBackpack.RootAttachment
  123.                 newWC.Part1 = descendant
  124.                 newWC.Parent = plrBackpack.RootAttachment
  125.             end
  126.         end
  127.        
  128.         local backpackWeld = Instance.new("Weld")
  129.         backpackWeld.Name = "BACKPACK WELD"
  130.         backpackWeld.Part0 = char:FindFirstChild("UpperTorso") or char:FindFirstChild("Torso")
  131.         backpackWeld.Part1 = plrBackpack:WaitForChild("RootAttachment")
  132.         backpackWeld.Parent = char:FindFirstChild("UpperTorso") or char:FindFirstChild("Torso")
  133.        
  134.         plrBackpack.Parent = char
  135.     end)
  136.    
  137.  
  138.     local success, data = pcall(function()
  139.         return ds:GetAsync(plr.UserId)
  140.     end)
  141.  
  142.     if success then
  143.        
  144.         local cash = data and data.Cash or 0
  145.         local pickaxe = data and data.Pickaxe or "Basic Pickaxe"
  146.         local backpack = data and data.Backpack or "Basic Backpack"
  147.         local backpackContentsData = data and data.BackpackContents or {}
  148.        
  149.         cashValue.Value = cash
  150.         pickaxeValue.Value = pickaxe
  151.         backpackValue.Value = backpack
  152.        
  153.         for ore, amount in pairs(backpackContentsData) do
  154.             backpackContents[ore].Value = amount
  155.         end
  156.        
  157.         print("Data successfully loaded for " .. plr.Name)
  158.        
  159.     else
  160.         warn("Data not loaded for " .. plr.Name)
  161.  
  162.         local failedToLoad = Instance.new("StringValue")
  163.         failedToLoad.Name = "FAILED TO LOAD DATA"
  164.         failedToLoad.Parent = plr
  165.  
  166.         re:FireClient(plr, "FAILED TO LOAD DATA")
  167.     end
  168. end)
  169.  
  170.  
  171. --Client requests
  172. local pickaxeCooldowns = {}
  173.  
  174. re.OnServerEvent:Connect(function(plr, instruction, data)
  175.    
  176.     if instruction == "BUY PICKAXE" then
  177.        
  178.         local cash = plr.leaderstats.Cash
  179.         local plrPickaxe = plr.Equipment.Pickaxe
  180.        
  181.         local newPickaxe = data[1]
  182.        
  183.         if pickaxes:FindFirstChild(newPickaxe) and plrPickaxe.Value ~= newPickaxe then
  184.             local price = pickaxesConfig[newPickaxe].cost
  185.            
  186.             if price <= cash.Value then
  187.                 cash.Value -= price
  188.                
  189.                 for i, tool in pairs(plr.Backpack:GetChildren()) do
  190.                     if tool.Name == plrPickaxe.Value then
  191.                         tool:Destroy()
  192.                     end
  193.                 end
  194.                 if plr.Character then
  195.                     for i, child in pairs(plr.Character:GetChildren()) do
  196.                         if child:IsA("Tool") and child.Name == plrPickaxe.Value then
  197.                             child:Destroy()
  198.                         end
  199.                     end
  200.                 end
  201.                
  202.                 plrPickaxe.Value = newPickaxe
  203.                 pickaxes[plrPickaxe.Value]:Clone().Parent = plr.Backpack
  204.             end
  205.         end
  206.        
  207.     elseif instruction == "BUY BACKPACK" then
  208.        
  209.         local cash = plr.leaderstats.Cash
  210.         local plrBackpack = plr.Equipment.Backpack
  211.  
  212.         local newBackpack = data[1]
  213.  
  214.         if backpacks:FindFirstChild(newBackpack) and plrBackpack.Value ~= newBackpack then
  215.             local price = backpacksConfig[newBackpack].cost
  216.  
  217.             if price <= cash.Value then
  218.                 cash.Value -= price
  219.  
  220.                 plrBackpack.Value = newBackpack
  221.  
  222.                 if plr.Character then
  223.                     local existingBackpack = plr.Character:FindFirstChild("BACKPACK")
  224.                     if existingBackpack then existingBackpack:Destroy() end
  225.                    
  226.                     local newBackpackModel = backpacks[plrBackpack.Value]:Clone()
  227.                     newBackpackModel.Name = "BACKPACK"
  228.                    
  229.                     for i, descendant in pairs(plrBackpack:GetDescendants()) do
  230.                         if descendant:IsA("BasePart") and descendant ~= plrBackpack.RootAttachment then
  231.                             local newWC = Instance.new("WeldConstraint")
  232.                             newWC.Part0 = plrBackpack.RootAttachment
  233.                             newWC.Part1 = descendant
  234.                             newWC.Parent = plrBackpack.RootAttachment
  235.                         end
  236.                     end
  237.  
  238.                     local backpackWeld = Instance.new("Weld")
  239.                     backpackWeld.Name = "BACKPACK WELD"
  240.                     backpackWeld.Part0 = plr.Character:FindFirstChild("UpperTorso") or plr.Character:FindFirstChild("Torso")
  241.                     backpackWeld.Part1 = newBackpackModel:WaitForChild("RootAttachment")
  242.                     backpackWeld.Parent = plr.Character:FindFirstChild("UpperTorso") or plr.Character:FindFirstChild("Torso")
  243.                    
  244.                     newBackpackModel.Parent = plr.Character
  245.                 end
  246.             end
  247.         end
  248.        
  249.     elseif instruction == "MINE ORE" then
  250.        
  251.         if not pickaxeCooldowns[plr] then
  252.            
  253.             local char = plr.Character
  254.             local mouseTarget = data[1]
  255.             if char and char.Humanoid.Health > 0 and mouseTarget and mouseTarget.Parent == workspace:WaitForChild("SPAWNED ORES FOLDER") then
  256.                
  257.                 local distance = (char.HumanoidRootPart.Position - mouseTarget.Position).Magnitude
  258.                 if distance <= otherConfig.MaxOreRange then
  259.                    
  260.                     local backpackStorageSize = backpacksConfig[plr.Equipment.Backpack.Value].storage
  261.                    
  262.                     local oresInBackpack = 0
  263.                     local backpackContents = plr.BackpackContents
  264.                     for i, oreValue in pairs(backpackContents:GetChildren()) do
  265.                         oresInBackpack += oreValue.Value
  266.                     end
  267.                    
  268.                     if oresInBackpack < backpackStorageSize then
  269.                        
  270.                         if char:FindFirstChildOfClass("Tool") and char:FindFirstChildOfClass("Tool").Name == plr.Equipment.Pickaxe.Value then
  271.                             pickaxeCooldowns[plr] = true
  272.                            
  273.                             local swingAnim = char.Humanoid.Animator:LoadAnimation(animations.PickaxeSwing)
  274.                             swingAnim:Play()
  275.                            
  276.                             local pickaxeDamage = pickaxesConfig[plr.Equipment.Pickaxe.Value].damage
  277.                             mouseTarget.HEALTH.Value -= pickaxeDamage
  278.                            
  279.                             if mouseTarget.HEALTH.Value <= 0 then
  280.                                
  281.                                 local oreName = mouseTarget.Name
  282.                                 plr.BackpackContents[oreName].Value += 1
  283.                                
  284.                                 mouseTarget:Destroy()
  285.                             end
  286.                            
  287.                             task.wait(pickaxesConfig[plr.Equipment.Pickaxe.Value].cooldown)
  288.                             pickaxeCooldowns[plr] = false
  289.                         end
  290.                     end
  291.                 end
  292.             end
  293.         end
  294.     end
  295. end)
  296.  
  297.  
  298. --Sell ores
  299. local touchDebounce = {}
  300.  
  301. areas:WaitForChild("SellZone").Touched:Connect(function(hit)
  302.     local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
  303.    
  304.     if plr and not touchDebounce[plr] then
  305.         touchDebounce[plr] = true
  306.        
  307.         local totalMinedValue = 0
  308.        
  309.         for i, oreValue in pairs(plr.BackpackContents:GetChildren()) do
  310.             local oreCost = oresConfig[oreValue.Name] and oresConfig[oreValue.Name].cost or stonesConfig[oreValue.Name] and stonesConfig[oreValue.Name].cost
  311.             totalMinedValue += oreValue.Value * oreCost
  312.            
  313.             oreValue.Value = 0
  314.         end
  315.        
  316.         local pickaxeMultiplier = pickaxesConfig[plr.Equipment.Pickaxe.Value].multiplier
  317.         totalMinedValue *= pickaxeMultiplier
  318.        
  319.         plr.leaderstats.Cash.Value += totalMinedValue
  320.        
  321.         task.wait(3)
  322.         touchDebounce[plr] = false
  323.     end
  324. end)
  325.  
  326.  
  327. --Spawn quarry
  328. while true do
  329.    
  330.     local workspaceOresFolder = workspace:FindFirstChild("SPAWNED ORES FOLDER") or Instance.new("Folder")
  331.     workspaceOresFolder.Name = "SPAWNED ORES FOLDER"
  332.     workspaceOresFolder.Parent = workspace
  333.    
  334.     workspaceOresFolder:ClearAllChildren()
  335.    
  336.     local oreZone = areas:WaitForChild("OreZone")
  337.    
  338.     for i, plr in pairs(game.Players:GetPlayers()) do
  339.         local char = plr.Character
  340.         if char and char.HumanoidRootPart.Position.Y <= oreZone.Position.Y then
  341.             plr:LoadCharacter()
  342.         end
  343.     end
  344.    
  345.    
  346.     local oresWide = math.ceil(oreZone.Size.X / stones["Light Stone"].Size.X)
  347.     local oresLong = math.ceil(oreZone.Size.Z / stones["Light Stone"].Size.Z)
  348.     local oresDeep = otherConfig.OreDepth
  349.    
  350.     local oreStart = oreZone.Position + Vector3.new(-oreZone.Size.X/2, oreZone.Size.Y/2, -oreZone.Size.Z/2) + Vector3.new(stones["Light Stone"].Size.X/2, -stones["Light Stone"].Size.Y/2, stones["Light Stone"].Size.Z/2)
  351.    
  352.     for y = 0, (oresDeep - 1) do
  353.         for x = 0, (oresWide - 1) do
  354.             for z = 0, (oresLong - 1) do
  355.                
  356.                 local orePosition = oreStart + Vector3.new(x * stones["Light Stone"].Size.X, -y * stones["Light Stone"].Size.Y, z * stones["Light Stone"].Size.Z)
  357.                
  358.                 local availableOres = {}
  359.                 for i, ore in pairs(ores) do
  360.                     local config = oresConfig[ore.Name] or stonesConfig[ore.Name]
  361.                     if config.minDepth <= y and config.maxDepth >= y then
  362.                         for n = 1, config.chance do
  363.                             table.insert(availableOres, ore)
  364.                         end
  365.                     end
  366.                 end
  367.                
  368.                 local chosenOre = availableOres[Random.new():NextInteger(1, #availableOres)]:Clone()
  369.                 chosenOre.Position = orePosition
  370.                 chosenOre.Parent = workspaceOresFolder
  371.                
  372.                 local oreHealth = Instance.new("NumberValue")
  373.                 oreHealth.Name = "HEALTH"
  374.                 oreHealth.Value = oresConfig[chosenOre.Name] and oresConfig[chosenOre.Name].health or stonesConfig[chosenOre.Name].health
  375.                 oreHealth.Parent = chosenOre
  376.             end
  377.         end
  378.     end
  379.    
  380.     task.wait(otherConfig.QuarryRefreshTime)
  381. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement