Hello!
I am creating a Fortnite Building System and I have ran into an error.
So, upon pressing the [Q and V] keyblinds, the ramp and wall generate, however when I press C the floor does not generate.
I am getting no errors in the output and I tried to debug it myself but couldn’t find the answer.
Module script:
local BuildManagerComponant = {}
BuildManagerComponant.GridSize = 16
BuildManagerComponant.BuildDistance = 2
BuildManagerComponant.isBuilding = false
BuildManagerComponant.SelectedBuild = "Wall"
local BuildingKeyblinds = {
[Enum.KeyCode.Q] = "Wall",
[Enum.KeyCode.C] = "Floor",
[Enum.KeyCode.V] = "Ramp"
}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuildingEvents = ReplicatedStorage:WaitForChild("BuildingEvents")
local PlaceBuild = BuildingEvents:WaitForChild("PlaceBuild")
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
function BuildManagerComponant.GetNextBuildPosition(HumanoidRootPartPosition, MouseLookVector3)
local DirectionVector3 = MouseLookVector3 * BuildManagerComponant.BuildDistance
DirectionVector3 += HumanoidRootPartPosition
return Vector3.new(
GridSnap(DirectionVector3.X, BuildManagerComponant.GridSize),
GridSnap(DirectionVector3.Y, BuildManagerComponant.GridSize),
GridSnap(DirectionVector3.Z, BuildManagerComponant.GridSize)
)
end
function BuildManagerComponant.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
function BuildManagerComponant.ToggleBuildMode(ActionName, InputState, InputObject)
if ActionName == "ToggleBuild" then
if InputState == Enum.UserInputState.Begin then
BuildManagerComponant.isBuilding = not BuildManagerComponant.isBuilding
warn("Toggled Build Mode: ", BuildManagerComponant.isBuilding)
end
end
end
function BuildManagerComponant.SwitchBuild(ActionName, InputState, InputObject)
if (ActionName == "SwitchBuild") then
if (InputState == Enum.UserInputState.Begin) then
BuildManagerComponant.isBuilding = true
if(BuildManagerComponant.isBuilding and BuildingKeyblinds[InputObject.KeyCode]) then
BuildManagerComponant.SelectedBuild = BuildingKeyblinds[InputObject.KeyCode]
end
end
end
end
function BuildManagerComponant.PlaceBuild(BuildMesh,BuildName)
if(BuildManagerComponant.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
PlaceBuild:FireServer(BuildName, BuildMesh.Position, BuildMesh.Orientation)
end
end
end
return BuildManagerComponant
Local script:
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local BuildManagerComponant = require(script:WaitForChild("BuildManagerComponant"))
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local CharacterModel = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = CharacterModel:WaitForChild("HumanoidRootPart")
local BuildingMeshes = script:WaitForChild("BuildMeshes")
local PreviewFolder = BuildingMeshes:WaitForChild("Preview")
local BuildingMeshes = {
["Wall"] = PreviewFolder:WaitForChild("Wall"),
["Floor"] = PreviewFolder:WaitForChild("Floor"),
["Ramp"] = PreviewFolder:WaitForChild("Ramp")
}
local CFrameAddOns = {
["Wall"] = CFrame.new(0,0,(BuildManagerComponant.GridSize/2)),
["Ramp"] = CFrame.new(0,0,0),
["Floor"] = CFrame.new(Vector3.new(0,-BuildManagerComponant.GridSize/2,0))
}
local function ResetPreviewParents(Mesh)
for _,BuildMesh in pairs(BuildingMeshes) do
if BuildMesh ~= Mesh and BuildMesh.Parent ~= PreviewFolder then
BuildMesh.Parent = PreviewFolder
end
end
end
ContextActionService:BindAction("SwitchBuild", BuildManagerComponant.SwitchBuild, true, Enum.KeyCode.Q, Enum.KeyCode.C, Enum.KeyCode.V)
ContextActionService:BindAction("ToggleBuild", BuildManagerComponant.ToggleBuildMode, true, Enum.KeyCode.H)
UserInputService.InputBegan:Connect(function(InputObject, GameProcessed)
if GameProcessed then return end
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
BuildManagerComponant.PlaceBuild(BuildingMeshes[BuildManagerComponant.SelectedBuild], BuildManagerComponant.SelectedBuild)
end
end)
RunService.RenderStepped:Connect(function()
if (BuildManagerComponant.isBuilding) then
local BuildComponantPosition = BuildManagerComponant.GetNextBuildPosition(HumanoidRootPart.Position, Mouse.Hit.LookVector)
local BuildComponantRotation = BuildManagerComponant.GetNextBuildRotation(Mouse.Hit.LookVector)
local BuildComponant = BuildingMeshes[BuildManagerComponant.SelectedBuild]
ResetPreviewParents(BuildComponant)
BuildComponant.Parent = workspace:FindFirstChild("Builds") or workspace
BuildComponant.CFrame = CFrame.new(BuildComponantPosition) * CFrame.Angles(BuildComponantRotation.X,BuildComponantRotation.Y,BuildComponantRotation.Z) * CFrameAddOns[BuildManagerComponant.SelectedBuild]
else
end
end)
Server script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuildingEventsFolder = ReplicatedStorage:WaitForChild("BuildingEvents")
local PlaceBuild = BuildingEventsFolder:WaitForChild("PlaceBuild")
PlaceBuild.OnServerEvent:Connect(function(Player, BuildName, BuildPosition, BuildRotation)
local BuildComponant = script:FindFirstChild(BuildName)
if BuildComponant then
BuildComponant = BuildComponant:Clone()
BuildComponant.Parent = workspace:FindFirstChild("Builds") or workspace
BuildComponant.Position = BuildPosition
BuildComponant.Orientation = BuildRotation
BuildComponant.Anchored = true
BuildComponant.CanCollide = true
end
end)
Apologies for the length of the code.
If anyone could help me, I would be grateful.