I got a build system from a YouTube tutorial which I modified to use a tool, I need help on destroying builds that clearly have to support and preventing the play from building if there is no support.
my pants decided not to load in apparently either
--ServerScriptService
--|| SERVICES ||--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--|| FOLDERS ||--
local BuildingEventsFolder = ReplicatedStorage:WaitForChild("BuildingEvents")
--|| REMOTE EVENTS ||--
local PlaceBuildEvent = BuildingEventsFolder:WaitForChild("PlaceBuild")
--|| EVENTS ||--
PlaceBuildEvent.OnServerEvent:Connect(function(Player, BuildName, BuildPosition, BuildRotation)
local BuildComponent = script:FindFirstChild(BuildName)
if BuildComponent then
BuildComponent = BuildComponent:Clone()
BuildComponent.Parent = workspace:FindFirstChild("Builds") or workspace
BuildComponent.Position = BuildPosition
BuildComponent:WaitForChild("Builder").Value = Player
BuildComponent.Orientation = BuildRotation
BuildComponent.Anchored = true
BuildComponent.CanCollide = true
end
end)
BuildingEventsFolder.DeleteBuild.OnServerEvent:Connect(function(player, build)
build:Destroy()
end)
-- MainBuildingScript located in starterpack
--|| SERVICES ||--
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
--|| MODULES ||--
local BuildManagerComponentModule = require(script:WaitForChild("BuildManagerComponent"))
--|| VARIABLES ||--
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local CharacterModel = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = CharacterModel:WaitForChild("HumanoidRootPart")
--|| FOLDERS ||--
local BuildMeshesFolder = script:WaitForChild("BuildMeshes")
local PreviewFolder = BuildMeshesFolder:WaitForChild("Preview")
local SFX = game.Workspace:WaitForChild("SFX")
--|| SETTINGS ||--
local BuildingMeshes = {
["Wall"] = PreviewFolder:WaitForChild("Wall"),
["Floor"] = PreviewFolder:WaitForChild("Floor"),
["Ramp"] = PreviewFolder:WaitForChild("Ramp"),
}
local CFrameAddOns = {
["Wall"] = CFrame.new(0,0,((BuildManagerComponentModule.GridSize/2)-.2)),
["Ramp"] = CFrame.new(0,0,0),
["Floor"] = CFrame.new(Vector3.new(0,(-BuildManagerComponentModule.GridSize/2)-.1,0))
}
--|| PRIVATE FUNCTIONS ||--
local function ResetPreviewParents(Mesh)
for _,BuildMesh in pairs(BuildingMeshes) do
if BuildMesh ~= Mesh and BuildMesh.Parent ~= PreviewFolder then
BuildMesh.Parent = PreviewFolder
end
end
end
--|| ACTIONS ||--
--|| EVENTS ||--
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
if GameProcessed then return end
if(InputObject.UserInputType == Enum.UserInputType.MouseButton1) then
BuildManagerComponentModule.PlaceBuild(BuildingMeshes[BuildManagerComponentModule.SelectedBuild], BuildManagerComponentModule.SelectedBuild)
end
end)
RunService.RenderStepped:Connect(function()
if(BuildManagerComponentModule.isBuilding) then
local BuildComponentPosition = BuildManagerComponentModule.GetNextBuildPosition(HumanoidRootPart.Position, Mouse.Hit.LookVector)
local BuildComponentRotation = BuildManagerComponentModule.GetNextBuildRotation(Mouse.Hit.LookVector)
local BuildComponent = BuildingMeshes[BuildManagerComponentModule.SelectedBuild]
ResetPreviewParents(BuildComponent)
BuildComponent.Parent = workspace:FindFirstChild("Builds") or workspace
BuildComponent.CFrame = CFrame.new(BuildComponentPosition) * CFrame.Angles(BuildComponentRotation.X,BuildComponentRotation.Y,BuildComponentRotation.Z) * CFrameAddOns[BuildManagerComponentModule.SelectedBuild]
else
ResetPreviewParents()
end
end)
-- BuildManagerComponent located in main building script
local BuildManagerComponent = {}
--|| MODULE SETTINGS ||--
BuildManagerComponent.GridSize = 10
BuildManagerComponent.BuildDistance = 5
--|| MODULE VARIABLES ||--
BuildManagerComponent.isBuilding = false
BuildManagerComponent.SelectedBuild = "Wall"
--|| TABLES & DICTIONARIES ||--
local BuildingKeybinds = {
}
local BuildPhrases = {
}
local player = game.Players.LocalPlayer
--|| SERVICES ||--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--|| FOLDERS ||--
local BuildingEventsFolder = ReplicatedStorage:WaitForChild("BuildingEvents")
local SFX = game.Workspace:WaitForChild("SFX")
--|| REMOTE EVENTS ||--
local PlaceBuildEvent = BuildingEventsFolder:WaitForChild("PlaceBuild")
--|| PRIVATE FUNCTIONS ||--
local function GridSnap(Value, Size)
return (math.floor(Value/Size + 0.5) * Size)
end
local function GetTouchingParts(Part)
local Connection = Part.Touched:Connect(function() end)
local Results = Part:GetTouchingParts()
Connection:Disconnect()
return Results
end
--|| MODULE FUNCTIONS ||--
function BuildManagerComponent.GetNextBuildPosition(HumanoidRootPartPosition, MouseLookVector3)
local DirectionVector3 = MouseLookVector3 * BuildManagerComponent.BuildDistance
DirectionVector3 += HumanoidRootPartPosition
return Vector3.new(
GridSnap(DirectionVector3.X, BuildManagerComponent.GridSize),
GridSnap(DirectionVector3.Y, BuildManagerComponent.GridSize) + BuildManagerComponent.GridSize/2,
GridSnap(DirectionVector3.Z, BuildManagerComponent.GridSize)
)
end
function BuildManagerComponent.GetNextBuildRotation(Vector)
if(typeof(Vector) == "Vector3") then
local Y = math.atan2(Vector.X, Vector.Z)
return Vector3.new(0,GridSnap(Y, math.rad(-90)), 0)
end
end
--|| TOGGLE BUILDING ||--
function BuildManagerComponent.ToggleBuildMode(ActionName, InputState, InputObject)
if ActionName == "ToggleBuild" then
if InputState == Enum.UserInputState.Begin then
BuildManagerComponent.isBuilding = not BuildManagerComponent.isBuilding
warn("Toggled Build Mode: ", BuildManagerComponent.isBuilding)
end
end
end
BuildingEventsFolder.ToggleBuilding.Event:Connect(function()
BuildManagerComponent.isBuilding = not BuildManagerComponent.isBuilding
warn("Toggled Build Mode: ", BuildManagerComponent.isBuilding)
end)
--|| SWITCH TO A DIFFERENT BUILD( Ramp, Wall, Floor, Etc) ||--
function BuildManagerComponent.SwitchBuild(ActionName, InputState, InputObject)
if(ActionName == "SwitchBuild") then
if(InputState == Enum.UserInputState.Begin) then
BuildManagerComponent.isBuilding = true
if(BuildManagerComponent.isBuilding and BuildingKeybinds[InputObject.KeyCode]) then
BuildManagerComponent.SelectedBuild = BuildingKeybinds[InputObject.KeyCode]
end
end
end
end
BuildingEventsFolder.SwitchBuild.Event:Connect(function(Build)
if(BuildManagerComponent.isBuilding) then
BuildManagerComponent.SelectedBuild = Build
end
end)
function BuildManagerComponent.PlaceBuild(BuildMesh, BuildName)
if(BuildManagerComponent.isBuilding) then
local Results = GetTouchingParts(BuildMesh)
local Placeable = true
for _, Build in pairs(Results) do
if Build.Name == BuildMesh.Name then
if Build.Position == BuildMesh.Position then
Placeable = false
end
end
end
if(Placeable) then
PlaceBuildEvent:FireServer(BuildName, BuildMesh.Position, BuildMesh.Orientation)
SFX.Build:Play()
end
end
end
return BuildManagerComponent