File size: 2,924 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
-- src/ServerScriptService/VehicleLogisticsManager.server.lua

local CollectionService = game:GetService("CollectionService")
local PhysicsService = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local VehicleConfig = require(ReplicatedStorage:WaitForChild("Shared"):WaitForChild("VehicleConfig"))

-- Setup collision groups initially to prevent chaotic interactions if needed later
pcall(function()
    PhysicsService:RegisterCollisionGroup(VehicleConfig.CollisionGroups.Logs)
    PhysicsService:RegisterCollisionGroup(VehicleConfig.CollisionGroups.Vehicles)
    
    -- Ensure they are fully collidable so logs can rest on the vehicle bed
    PhysicsService:CollisionGroupSetCollidable(VehicleConfig.CollisionGroups.Logs, VehicleConfig.CollisionGroups.Vehicles, true)
end)

-- Function to initialize a new vehicle's physics to handle the heavy logs safely
local function setupVehicle(vehicleModel)
    -- Typically flatbeds are made of several parts grouped under a 'Flatbed' model or have a specific tag.
    -- We will check for parts named "FlatbedPart" or tagged "Flatbed" within the vehicle.
    local function applyToPart(part)
        if part:IsA("BasePart") then
            part.CustomPhysicalProperties = VehicleConfig.FlatbedPhysicalProperties
            part.CollisionGroup = VehicleConfig.CollisionGroups.Vehicles
        end
    end

    for _, descendant in pairs(vehicleModel:GetDescendants()) do
        if descendant.Name == "FlatbedPart" or CollectionService:HasTag(descendant, "Flatbed") then
            applyToPart(descendant)
        end
    end
end

-- Function to apply Log physics specifically for stability
local function setupLog(logSegment)
    if logSegment:IsA("BasePart") then
        logSegment.CollisionGroup = VehicleConfig.CollisionGroups.Logs
        
        -- Override elasticity so logs don't bounce out of the truck on rough terrain
        local currentProps = logSegment.CustomPhysicalProperties or PhysicalProperties.new(logSegment.Material)
        logSegment.CustomPhysicalProperties = PhysicalProperties.new(
            currentProps.Density, 
            0.8, -- Moderate friction against other logs
            0,   -- ZERO elasticity
            100, -- Friction weight
            100  -- Elasticity weight (forcing the zero elasticity to win against the truck)
        )
    end
end


-- Listen for new vehicles and logs added to the game
CollectionService:GetInstanceAddedSignal("Vehicle"):Connect(setupVehicle)
CollectionService:GetInstanceAddedSignal("TreeSegment"):Connect(setupLog)

-- Setup existing elements that might have spawned before this script loaded
for _, vehicle in pairs(CollectionService:GetTagged("Vehicle")) do
    setupVehicle(vehicle)
end

for _, log in pairs(CollectionService:GetTagged("TreeSegment")) do
    setupLog(log)
end