Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Load required services and modules
- local CollectionService = game:GetService("CollectionService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local ServerStorage = game:GetService("ServerStorage")
- local Recipes = require(ReplicatedStorage.Modules.Recipes).Recipes
- local SmeltingRecipes = require(ReplicatedStorage.Modules.SmeltingRecipes).Recipes
- local InventoryHandler = require(ServerStorage.Modules.InventoryHandler)
- local ItemHandler = require(ServerStorage.Modules.ItemHandler)
- local MiningHandler = require(ServerStorage.Modules.MiningHandler)
- local HTTP = game:GetService("HttpService")
- local wires = {}
- -- Handle the Wire event
- ReplicatedStorage.Wiring.Wire.OnServerEvent:Connect(function(player_fired, Type, Input, Output, c, d)
- local wire
- if Type == "Wire" then
- if Input ~= nil and Output ~= nil then
- coroutine.wrap(function()
- -- Check existing connections and valid positions
- local beam1 = Input:FindFirstChild("Beam")
- local beam2 = Output:FindFirstChild("Beam")
- if beam1 then
- if beam1.Attachment0 == Input and beam1.Attachment1 == Output then return end
- end
- if beam2 then
- if beam2.Attachment0 == Input and beam2.Attachment1 == Output then return end
- end
- if Input.Parent == Output.Parent then return end
- if not InventoryHandler.ItemExists(player_fired, "CoiledWire") then return end
- InventoryHandler.AddItem(player_fired, "CoiledWire", -1)
- -- Create and configure the wire
- wire = game:GetService("ReplicatedStorage").Wiring.Beam:Clone()
- wire.Parent = Output
- wire.Attachment0 = Input
- wire.Attachment1 = Output
- wire:SetAttribute("IsPowered", Output:GetAttribute("IsPowered"))
- Input:SetAttribute("IsPowered", Output:GetAttribute("IsPowered"))
- local owner = Instance.new("ObjectValue", wire)
- owner.Name = "WireOwner"
- owner.Value = player_fired
- table.insert(wires, wire)
- -- Manage wire length and breakage
- if wire:GetAttribute("IsPowered") then
- wire.Color = ColorSequence.new(Color3.fromRGB(0, 170, 255))
- else
- wire.Color = ColorSequence.new(Color3.fromRGB(0, 0, 0))
- end
- local connection = Output:GetAttributeChangedSignal("IsPowered"):Connect(function()
- local IsOutputPowered = Output:GetAttribute("IsPowered")
- wire:SetAttribute("IsPowered", IsOutputPowered)
- Input:SetAttribute("IsPowered", IsOutputPowered)
- if wire:GetAttribute("IsPowered") then
- wire.Color = ColorSequence.new(Color3.fromRGB(0, 170, 255))
- else
- wire.Color = ColorSequence.new(Color3.fromRGB(0, 0, 0))
- end
- end)
- while task.wait() do
- if wire.Parent == nil then
- connection:Disconnect()
- if Input:FindFirstChild("Beam") and Input.Beam:GetAttribute("IsPowered") then
- else
- Input:SetAttribute("IsPowered", false)
- end
- break
- else
- local mag = (wire.Attachment0.WorldPosition - wire.Attachment1.WorldPosition).magnitude
- if mag > 20 then
- local CoiledWireClonePosition
- if math.random(1,2) == 1 then
- CoiledWireClonePosition = wire.Attachment0.WorldPosition
- else
- CoiledWireClonePosition = wire.Attachment1.WorldPosition
- end
- if wire:FindFirstChild("WireOwner") then
- game:GetService("ReplicatedStorage").Wiring.Local:FireClient(wire.WireOwner.Value, "WireBroke_TooLong")
- end
- wire:Destroy()
- local CoiledWireClone = game:GetService("ReplicatedStorage").Items.Electric.CoiledWire:Clone()
- CoiledWireClone.Parent = workspace
- CoiledWireClone.Position = CoiledWireClonePosition
- CoiledWireClone.Anchored = false
- coroutine.wrap(function()
- task.wait(2)
- pcall(function()
- CoiledWireClone.Anchored = true
- end)
- end)
- end
- end
- end
- end)()
- end
- end
- end)
- -- Handle the Delete event
- ReplicatedStorage.Wiring.Delete.OnServerEvent:Connect(function(player_fired, Beam)
- -- Handle wire deletion
- if Beam:IsA("Beam") and Beam.Parent:IsA("Attachment") then
- local CoiledWireClonePosition
- if math.random(1,2) == 1 then
- CoiledWireClonePosition = Beam.Attachment0.WorldPosition
- else
- CoiledWireClonePosition = Beam.Attachment1.WorldPosition
- end
- Beam:Destroy()
- local wire = ReplicatedStorage.Items.Electric.CoiledWire:Clone()
- wire.Position = CoiledWireClonePosition
- wire.Parent = workspace.Drops
- --ItemHandler.SpawnItem("CoiledWire", CoiledWireClonePosition)
- end
- end)
- -- Handle the Drop event
- ReplicatedStorage.Drop.OnServerEvent:Connect(function(player_fired, item, count)
- -- Handle item dropping
- if not count then
- count = 1
- end
- if not player_fired:GetAttribute("DropDebounce") then
- player_fired:SetAttribute("DropDebounce", true)
- local DecodedInventory = HTTP:JSONDecode(player_fired:GetAttribute("InventoryJSON"))
- if InventoryHandler.ItemCount(player_fired, item) >= count and count > 0 then
- InventoryHandler.DropItem(player_fired, item, count)
- end
- wait(0.1 * count)
- player_fired:SetAttribute("DropDebounce", false)
- end
- end)
- -- Handle the Equip event
- ReplicatedStorage.Equip.OnServerEvent:Connect(function(player_fired, item, count)
- -- Handle equipping items
- if not count then
- count = 1
- end
- local ItemExample = game:GetService("ReplicatedStorage").Items:FindFirstChild(item, true)
- if ItemExample:IsA("Tool") then
- local DecodedInventory = HTTP:JSONDecode(player_fired:GetAttribute("InventoryJSON"))
- if InventoryHandler.ItemCount(player_fired, item) >= count and count > 0 then
- InventoryHandler.AddItem(player_fired, item, -count)
- local ItemClone = game:GetService("ReplicatedStorage").Items:FindFirstChild(item, true):Clone()
- ItemClone.Parent = player_fired.Backpack
- end
- end
- end)
- -- Handle the Craft event
- ReplicatedStorage.Craft.OnServerEvent:Connect(function(player_fired, item)
- -- Handle crafting items
- local ItemCraft = Recipes[item].Items
- local AllowedToCraft = true
- for i,v in pairs(ItemCraft) do
- if InventoryHandler.ItemCount(player_fired, i) >= v then
- InventoryHandler.AddItem(player_fired, i, -v)
- else
- AllowedToCraft = false
- end
- end
- if AllowedToCraft then
- InventoryHandler.AddItem(player_fired, item, 1)
- end
- end)
- -- Handle the AddToCraftingQueue event
- ReplicatedStorage.AddToCraftingQueue.OnServerEvent:Connect(function(player_fired, itemToCraft)
- -- Handle adding an item to the crafting queue
- local craftingQueue = HTTP:JSONDecode(player_fired:GetAttribute("CraftingQueue") or "[]")
- table.insert(craftingQueue, itemToCraft)
- player_fired:SetAttribute("CraftingQueue", HTTP:JSONEncode(craftingQueue))
- end)
- -- Handle the ProcessCraftingQueue event
- local function ProcessCraftingQueue(player)
- local craftingQueue = HTTP:JSONDecode(player:GetAttribute("CraftingQueue") or "[]")
- local updatedQueue = {}
- for _, itemToCraft in ipairs(craftingQueue) do
- local recipe = Recipes[itemToCraft]
- local canCraft = true
- -- Check if the player has the required items for crafting
- for requiredItem, requiredCount in pairs(recipe.Items) do
- if InventoryHandler.ItemCount(player, requiredItem) < requiredCount then
- canCraft = false
- break
- end
- end
- if canCraft then
- -- Deduct required items and add the crafted item
- for requiredItem, requiredCount in pairs(recipe.Items) do
- InventoryHandler.AddItem(player, requiredItem, -requiredCount)
- end
- InventoryHandler.AddItem(player, itemToCraft, 1)
- else
- -- Move items to the next queue if crafting is not possible
- table.insert(updatedQueue, itemToCraft)
- end
- end
- player:SetAttribute("CraftingQueue", HTTP:JSONEncode(updatedQueue))
- end
- -- Process crafting queue periodically
- local queueProcessInterval = 300 -- seconds (adjust as needed)
- game:GetService("RunService").Heartbeat:Connect(function(dt)
- for _, player in ipairs(game.Players:GetPlayers()) do
- local lastProcessedTime = player:GetAttribute("LastCraftingQueueProcessTime") or 0
- if os.time() - lastProcessedTime >= queueProcessInterval then
- ProcessCraftingQueue(player)
- player:SetAttribute("LastCraftingQueueProcessTime", os.time())
- end
- end
- end)
- -- Handle the Mine event
- ReplicatedStorage.Mine.OnServerEvent:Connect(function(player_fired, ore)
- -- Handle mining ores
- local ToolName = player_fired.Character:FindFirstChildWhichIsA("Tool").Name
- MiningHandler.DamageOre(ToolName, ore)
- end)
- -- Handle the Chop event
- ReplicatedStorage.Chop.OnServerEvent:Connect(function(player_fired, tree)
- -- Handle chopping trees
- local ToolName = player_fired.Character:FindFirstChildWhichIsA("Tool").Name
- MiningHandler.DamageTree(ToolName, tree)
- end)
- -- Handle the Smelt event
- ReplicatedStorage.Smelt.OnServerEvent:Connect(function(player_fired, furnace, item)
- -- Handle smelting items
- if furnace ~= nil then
- local Queue = HTTP:JSONDecode(furnace:GetAttribute("ItemQueue"))
- local CurrentlySmelting = furnace:GetAttribute("Smelting")
- local ItemCraft = SmeltingRecipes[item]
- local AllowedToCraft = true
- local SmeltSuccess = true
- for i,v in pairs(ItemCraft) do
- if InventoryHandler.ItemCount(player_fired, i) >= v then
- InventoryHandler.AddItem(player_fired, i, -v)
- else
- AllowedToCraft = false
- end
- end
- if AllowedToCraft then
- table.insert(Queue, item)
- end
- furnace:SetAttribute("ItemQueue", HTTP:JSONEncode(Queue))
- end
- end)
- -- Handle the FurnaceTake event
- ReplicatedStorage.FurnaceTake.OnServerEvent:Connect(function(player_fired, furnace)
- -- Handle taking items from a furnace
- if furnace ~= nil then
- local ItemsInside = HTTP:JSONDecode(furnace:GetAttribute("ItemsInside"))
- for i,v in pairs(ItemsInside) do
- InventoryHandler.AddItem(player_fired, i, v)
- end
- furnace:SetAttribute("ItemsInside", "{}")
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement