Help restricting building outside of the building limits

I want to restrict building once the build has gone outside of the players baseplate, I’ve tried a few methods but none have worked

-- Build Manager Component Module Script
local BuildManagerComponent = {}
--|| MODULE SETTINGS ||--
BuildManagerComponent.GridSize = 10
BuildManagerComponent.BuildDistance = 1
--|| MODULE VARIABLES ||--
BuildManagerComponent.isBuilding = false
BuildManagerComponent.SelectedBuild = "Wall"
--|| TABLES & DICTIONARIES ||--
local player = game.Players.LocalPlayer
local LimitRefrence = game.Workspace:WaitForChild("BuildingPlates"):WaitForChild("Plate"..player:WaitForChild("playerNumber").Value)["Build Limit"]
print(LimitRefrence)

local BuildingKeybinds = {
}


local BuildPhrases = {
	
}


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

This will all be about the math in relation to the baseplate.
You have to get the baseplate’s position in the world, and use that to determine if a part being placed is within the build area.

So if my baseplate is at 10,10 on the x and z, and my baseplate is 100 studs by 100 studs
Any part being placed by me, should only be allowed if it is in the world position between -40,-40 x,z and 60,60 x and z

baseplatePosition - (baseplateSize / 2) for the minimum
and minimum + baseplateSize for the maximum

local minCornerPos = Vector3.new(-10,-10,-10)
local maxCornerPos = Vector3.new(10,10,10)

local function isCharacterInArea(character)
local pos = character.HumanoidRootPart.Position

    if minCornerPos.X < pos.X < maxCornerPos.X then
      if minCornerPos.Y < pos.Y < maxCornerPos.Y then
        if minCornerPos.Z < pos.Z < maxCornerPos.Z then
        print("Character is in zone")
        return true
        end
      end
    end
end

Wrote this an hour ago for someone who wanted to check if a player is within a certain zone. You can use this too. If the part is outside of these bounds, stop moving it and make it red

Thank you for your help, it works great :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.