File size: 2,976 Bytes
0712d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- 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