Im making a build system and i want it so you cant build outside a the given space, i dont know how to do this (i want to be able to check if the block is within this space befor it is placed)
3 Likes
Heres and example:
local minX = -50
local maxX = 50
local minY = 0
local maxY = 30
local minZ = -50
local maxZ = 50
local function isPositionWithinBuildArea(position)
return position.x >= minX and position.x <= maxX and
position.y >= minY and position.y <= maxY and
position.z >= minZ and position.z <= maxZ
end
local function handleBlockPlacement(player, block, position)
if isPositionWithinBuildArea(position) then block.Parent = workspace
else
warn("Cannot place block outside the valid build area.")
end
end
local player = game.Players.LocalPlayer
local block = Instance.new("Part")
block.Size = Vector3.new(1, 1, 1)
local position = Vector3.new(10, 5, 10)
handleBlockPlacement(player, block, position)
Change want you want to change, and make sure everything is customized to your needs.
2 Likes
it always prints, Cannot place block outside the valid build area.
local function isPositionWithinBuildArea(position)
return position.x >= game.Workspace.End.Position.X and position.x <= game.Workspace.Start.Position.X and
position.y >= game.Workspace.End.Position.Y and position.y <= game.Workspace.Start.Position.Y and
position.z >= game.Workspace.End.Position.Z and position.z <= game.Workspace.Start.Position.Z
end
1 Like
Fixed it,
here is fixed code
local function isPositionWithinBuildArea(position)
local XTable = {game.Workspace.End.Position.X, game.Workspace.Start.Position.X}
local YTable = {game.Workspace.End.Position.Y, game.Workspace.Start.Position.Y}
local ZTable = {game.Workspace.End.Position.Z, game.Workspace.Start.Position.Z}
local minX = math.min(table.unpack(XTable))
local minY = math.min(table.unpack(YTable))
local minZ = math.min(table.unpack(ZTable))
local maxX = math.max(table.unpack(XTable))
local maxY = math.max(table.unpack(YTable))
local maxZ = math.max(table.unpack(ZTable))
return position.x >= minX and position.x <= maxX and
position.y >= minY and position.y <= maxY and
position.z >= minZ and position.z <= maxZ
end
Happy to help. Feel free to reach out to me if you have anymore questions.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.