Advertisement
Scripting_King

Request For Hire

Aug 12th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.72 KB | Source Code | 0 0
  1. -- Load required services and modules
  2. local CollectionService = game:GetService("CollectionService")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local ServerStorage = game:GetService("ServerStorage")
  5. local Recipes = require(ReplicatedStorage.Modules.Recipes).Recipes
  6. local SmeltingRecipes = require(ReplicatedStorage.Modules.SmeltingRecipes).Recipes
  7. local InventoryHandler = require(ServerStorage.Modules.InventoryHandler)
  8. local ItemHandler = require(ServerStorage.Modules.ItemHandler)
  9. local MiningHandler = require(ServerStorage.Modules.MiningHandler)
  10. local HTTP = game:GetService("HttpService")
  11. local wires = {}
  12. -- Handle the Wire event
  13. ReplicatedStorage.Wiring.Wire.OnServerEvent:Connect(function(player_fired, Type, Input, Output, c, d)
  14.     local wire
  15.     if Type == "Wire" then
  16.         if Input ~= nil and Output ~= nil then
  17.             coroutine.wrap(function()
  18.                 -- Check existing connections and valid positions
  19.                 local beam1 = Input:FindFirstChild("Beam")
  20.                 local beam2 = Output:FindFirstChild("Beam")
  21.                 if beam1 then
  22.                     if beam1.Attachment0 == Input and beam1.Attachment1 == Output then return end
  23.                 end
  24.                 if beam2 then
  25.                     if beam2.Attachment0 == Input and beam2.Attachment1 == Output then return end
  26.                 end
  27.                 if Input.Parent == Output.Parent then return end
  28.                 if not InventoryHandler.ItemExists(player_fired, "CoiledWire") then return end
  29.                 InventoryHandler.AddItem(player_fired, "CoiledWire", -1)
  30.                 -- Create and configure the wire
  31.                 wire = game:GetService("ReplicatedStorage").Wiring.Beam:Clone()
  32.                 wire.Parent = Output
  33.                 wire.Attachment0 = Input
  34.                 wire.Attachment1 = Output
  35.                 wire:SetAttribute("IsPowered", Output:GetAttribute("IsPowered"))
  36.                 Input:SetAttribute("IsPowered", Output:GetAttribute("IsPowered"))
  37.                 local owner = Instance.new("ObjectValue", wire)
  38.                 owner.Name = "WireOwner"
  39.                 owner.Value = player_fired
  40.                 table.insert(wires, wire)
  41.                 -- Manage wire length and breakage
  42.                 if wire:GetAttribute("IsPowered") then
  43.                     wire.Color = ColorSequence.new(Color3.fromRGB(0, 170, 255))
  44.                 else
  45.                     wire.Color = ColorSequence.new(Color3.fromRGB(0, 0, 0))
  46.                 end
  47.                 local connection = Output:GetAttributeChangedSignal("IsPowered"):Connect(function()
  48.                     local IsOutputPowered = Output:GetAttribute("IsPowered")
  49.                     wire:SetAttribute("IsPowered", IsOutputPowered)
  50.                     Input:SetAttribute("IsPowered", IsOutputPowered)
  51.                     if wire:GetAttribute("IsPowered") then
  52.                         wire.Color = ColorSequence.new(Color3.fromRGB(0, 170, 255))
  53.                     else
  54.                         wire.Color = ColorSequence.new(Color3.fromRGB(0, 0, 0))
  55.                     end                
  56.                 end)                                       
  57.                     while task.wait() do
  58.                         if wire.Parent == nil then
  59.                             connection:Disconnect()
  60.                             if Input:FindFirstChild("Beam") and Input.Beam:GetAttribute("IsPowered") then
  61.                             else
  62.                                 Input:SetAttribute("IsPowered", false)  
  63.                             end
  64.  
  65.                             break
  66.                         else
  67.                             local mag = (wire.Attachment0.WorldPosition - wire.Attachment1.WorldPosition).magnitude
  68.                             if mag > 20 then
  69.                                 local CoiledWireClonePosition
  70.                                 if math.random(1,2) == 1 then
  71.                                     CoiledWireClonePosition = wire.Attachment0.WorldPosition
  72.                                 else
  73.                                     CoiledWireClonePosition = wire.Attachment1.WorldPosition
  74.                                 end
  75.                                 if wire:FindFirstChild("WireOwner") then
  76.                                     game:GetService("ReplicatedStorage").Wiring.Local:FireClient(wire.WireOwner.Value, "WireBroke_TooLong")
  77.                                 end
  78.                                 wire:Destroy()
  79.                                 local CoiledWireClone = game:GetService("ReplicatedStorage").Items.Electric.CoiledWire:Clone()
  80.                                 CoiledWireClone.Parent = workspace
  81.                                 CoiledWireClone.Position = CoiledWireClonePosition
  82.                                 CoiledWireClone.Anchored = false
  83.  
  84.                                 coroutine.wrap(function()
  85.                                     task.wait(2)
  86.                                     pcall(function()
  87.                                         CoiledWireClone.Anchored = true
  88.                                 end)
  89.                             end)
  90.                         end
  91.  
  92.                     end
  93.                 end
  94.             end)()
  95.         end
  96.     end
  97. end)
  98. -- Handle the Delete event
  99. ReplicatedStorage.Wiring.Delete.OnServerEvent:Connect(function(player_fired, Beam)
  100.         -- Handle wire deletion
  101.     if Beam:IsA("Beam") and Beam.Parent:IsA("Attachment") then
  102.         local CoiledWireClonePosition
  103.         if math.random(1,2) == 1 then
  104.             CoiledWireClonePosition = Beam.Attachment0.WorldPosition
  105.         else
  106.             CoiledWireClonePosition = Beam.Attachment1.WorldPosition
  107.         end
  108.         Beam:Destroy()
  109.  
  110.         local wire = ReplicatedStorage.Items.Electric.CoiledWire:Clone()
  111.         wire.Position = CoiledWireClonePosition
  112.         wire.Parent = workspace.Drops
  113.         --ItemHandler.SpawnItem("CoiledWire", CoiledWireClonePosition)
  114.     end
  115. end)
  116. -- Handle the Drop event
  117. ReplicatedStorage.Drop.OnServerEvent:Connect(function(player_fired, item, count)
  118.     -- Handle item dropping
  119.     if not count then
  120.         count = 1
  121.     end
  122.     if not player_fired:GetAttribute("DropDebounce") then
  123.         player_fired:SetAttribute("DropDebounce", true)
  124.         local DecodedInventory = HTTP:JSONDecode(player_fired:GetAttribute("InventoryJSON"))
  125.         if InventoryHandler.ItemCount(player_fired, item) >= count and count > 0 then
  126.             InventoryHandler.DropItem(player_fired, item, count)
  127.         end
  128.         wait(0.1 * count)
  129.         player_fired:SetAttribute("DropDebounce", false)
  130.     end
  131. end)
  132. -- Handle the Equip event
  133. ReplicatedStorage.Equip.OnServerEvent:Connect(function(player_fired, item, count)
  134.     -- Handle equipping items
  135.     if not count then
  136.         count = 1
  137.     end
  138.     local ItemExample = game:GetService("ReplicatedStorage").Items:FindFirstChild(item, true)
  139.     if ItemExample:IsA("Tool") then
  140.         local DecodedInventory = HTTP:JSONDecode(player_fired:GetAttribute("InventoryJSON"))
  141.         if InventoryHandler.ItemCount(player_fired, item) >= count and count > 0 then
  142.             InventoryHandler.AddItem(player_fired, item, -count)
  143.             local ItemClone = game:GetService("ReplicatedStorage").Items:FindFirstChild(item, true):Clone()
  144.             ItemClone.Parent = player_fired.Backpack
  145.         end
  146.     end
  147. end)
  148. -- Handle the Craft event
  149. ReplicatedStorage.Craft.OnServerEvent:Connect(function(player_fired, item)
  150.     -- Handle crafting items
  151.     local ItemCraft = Recipes[item].Items
  152.     local AllowedToCraft = true
  153.     for i,v in pairs(ItemCraft) do
  154.         if InventoryHandler.ItemCount(player_fired, i) >= v then
  155.             InventoryHandler.AddItem(player_fired, i, -v)
  156.         else
  157.             AllowedToCraft = false
  158.         end
  159.     end
  160.     if AllowedToCraft then
  161.         InventoryHandler.AddItem(player_fired, item, 1)
  162.     end
  163. end)
  164. -- Handle the AddToCraftingQueue event
  165. ReplicatedStorage.AddToCraftingQueue.OnServerEvent:Connect(function(player_fired, itemToCraft)
  166.     -- Handle adding an item to the crafting queue
  167.     local craftingQueue = HTTP:JSONDecode(player_fired:GetAttribute("CraftingQueue") or "[]")
  168.     table.insert(craftingQueue, itemToCraft)
  169.     player_fired:SetAttribute("CraftingQueue", HTTP:JSONEncode(craftingQueue))
  170. end)
  171. -- Handle the ProcessCraftingQueue event
  172. local function ProcessCraftingQueue(player)
  173.     local craftingQueue = HTTP:JSONDecode(player:GetAttribute("CraftingQueue") or "[]")
  174.     local updatedQueue = {}
  175.     for _, itemToCraft in ipairs(craftingQueue) do
  176.         local recipe = Recipes[itemToCraft]
  177.         local canCraft = true
  178.         -- Check if the player has the required items for crafting
  179.         for requiredItem, requiredCount in pairs(recipe.Items) do
  180.             if InventoryHandler.ItemCount(player, requiredItem) < requiredCount then
  181.                 canCraft = false
  182.                 break
  183.             end
  184.         end
  185.         if canCraft then
  186.             -- Deduct required items and add the crafted item
  187.             for requiredItem, requiredCount in pairs(recipe.Items) do
  188.                 InventoryHandler.AddItem(player, requiredItem, -requiredCount)
  189.             end
  190.             InventoryHandler.AddItem(player, itemToCraft, 1)
  191.         else
  192.             -- Move items to the next queue if crafting is not possible
  193.             table.insert(updatedQueue, itemToCraft)
  194.         end
  195.     end
  196.     player:SetAttribute("CraftingQueue", HTTP:JSONEncode(updatedQueue))
  197. end
  198. -- Process crafting queue periodically
  199. local queueProcessInterval = 300 -- seconds (adjust as needed)
  200. game:GetService("RunService").Heartbeat:Connect(function(dt)
  201.     for _, player in ipairs(game.Players:GetPlayers()) do
  202.         local lastProcessedTime = player:GetAttribute("LastCraftingQueueProcessTime") or 0
  203.         if os.time() - lastProcessedTime >= queueProcessInterval then
  204.             ProcessCraftingQueue(player)
  205.             player:SetAttribute("LastCraftingQueueProcessTime", os.time())
  206.         end
  207.     end
  208. end)
  209. -- Handle the Mine event
  210. ReplicatedStorage.Mine.OnServerEvent:Connect(function(player_fired, ore)
  211.     -- Handle mining ores
  212.     local ToolName = player_fired.Character:FindFirstChildWhichIsA("Tool").Name
  213.     MiningHandler.DamageOre(ToolName, ore)
  214. end)
  215. -- Handle the Chop event
  216. ReplicatedStorage.Chop.OnServerEvent:Connect(function(player_fired, tree)
  217.     -- Handle chopping trees
  218.     local ToolName = player_fired.Character:FindFirstChildWhichIsA("Tool").Name
  219.     MiningHandler.DamageTree(ToolName, tree)
  220. end)
  221. -- Handle the Smelt event
  222. ReplicatedStorage.Smelt.OnServerEvent:Connect(function(player_fired, furnace, item)
  223.     -- Handle smelting items
  224.     if furnace ~= nil then
  225.         local Queue = HTTP:JSONDecode(furnace:GetAttribute("ItemQueue"))
  226.         local CurrentlySmelting = furnace:GetAttribute("Smelting")
  227.         local ItemCraft = SmeltingRecipes[item]
  228.         local AllowedToCraft = true
  229.         local SmeltSuccess = true
  230.         for i,v in pairs(ItemCraft) do
  231.             if InventoryHandler.ItemCount(player_fired, i) >= v then
  232.                 InventoryHandler.AddItem(player_fired, i, -v)
  233.             else
  234.                 AllowedToCraft = false
  235.             end
  236.         end
  237.         if AllowedToCraft then
  238.             table.insert(Queue, item)
  239.         end
  240.         furnace:SetAttribute("ItemQueue", HTTP:JSONEncode(Queue))
  241.     end
  242. end)
  243. -- Handle the FurnaceTake event
  244. ReplicatedStorage.FurnaceTake.OnServerEvent:Connect(function(player_fired, furnace)
  245.     -- Handle taking items from a furnace
  246.     if furnace ~= nil then
  247.         local ItemsInside = HTTP:JSONDecode(furnace:GetAttribute("ItemsInside"))
  248.         for i,v in pairs(ItemsInside) do
  249.             InventoryHandler.AddItem(player_fired, i, v)
  250.         end
  251.         furnace:SetAttribute("ItemsInside", "{}")
  252.     end
  253. end)
  254.  
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement