TimberWoods / src /ServerScriptService /SawmillManager.server.lua
algorembrant's picture
Upload 88 files
0712d5f verified
-- src/ServerScriptService/SawmillManager.server.lua
local CollectionService = game:GetService("CollectionService")
-- A generic processing function that converts a log into a plank
local function processLog(logPart, machineType)
local state = logPart:GetAttribute("ProcessState") or "Raw"
local treeType = logPart:GetAttribute("TreeType")
if machineType == "Stripper" and state == "Raw" then
-- Remove bark (Visual change: change material or color)
logPart:SetAttribute("ProcessState", "Stripped")
-- In a real game, you might tween the size down slightly to represent bark loss
local newSize = logPart.Size * 0.9
logPart.Size = newSize
logPart.Material = Enum.Material.Wood
elseif machineType == "Sawmill" and (state == "Raw" or state == "Stripped") then
-- Convert to a clean plank
logPart:SetAttribute("ProcessState", "Plank")
-- Change shape
local volume = logPart.Size.X * logPart.Size.Y * logPart.Size.Z
-- Make it rectangular and flat (like a plank) while preserving some volume proportionality
local plankThickness = 0.5
local length = logPart.Size.Y
local width = (volume / length) / plankThickness -- maintain roughly same mass/volume for physics sanity, minus some waste
-- Prevent extremely thin or extremely wide planks
width = math.clamp(width, 1, 4)
logPart.Size = Vector3.new(width, length, plankThickness)
logPart.Material = Enum.Material.WoodPlanks
logPart.BrickColor = BrickColor.new("Burlap") -- Typical plank color, or utilize TreeType specific clean color
-- Update collision and physics bounds instantly
logPart.Anchored = false
end
end
-- Monitor for touch events on Sawmill Triggers
local function bindMachine(machineModel)
local triggerPart = machineModel:FindFirstChild("ProcessTrigger")
if not triggerPart then return end
local machineType = machineModel:GetAttribute("MachineType") or "Sawmill"
local debounce = {}
triggerPart.Touched:Connect(function(hit)
-- Ensure it's a tree segment
if CollectionService:HasTag(hit, "TreeSegment") then
-- Prevent rapid firing
if not debounce[hit] then
debounce[hit] = true
-- Process it
processLog(hit, machineType)
-- Cleanup debounce
task.delay(1, function()
debounce[hit] = nil
end)
end
end
end)
end
-- Initialize existing and future machines
CollectionService:GetInstanceAddedSignal("ProcessingMachine"):Connect(bindMachine)
for _, machine in pairs(CollectionService:GetTagged("ProcessingMachine")) do
bindMachine(machine)
end