Scripting_King

My 2nd Script

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