-- 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