Skip to content

Instantly share code, notes, and snippets.

@MajorTal
Created May 31, 2025 16:13
Show Gist options
  • Save MajorTal/0f7930317d62803e9cb1b9f4bd6fb17b to your computer and use it in GitHub Desktop.
Save MajorTal/0f7930317d62803e9cb1b9f4bd6fb17b to your computer and use it in GitHub Desktop.
local tool = script.Parent
local placeRequest = tool:WaitForChild("PlaceRequest")
local GRID_SIZE = 4 -- Grid snapping size
local function snapToGrid(position, gridSize)
return Vector3.new(
math.floor(position.X / gridSize + 0.5) * gridSize,
position.Y,
math.floor(position.Z / gridSize + 0.5) * gridSize
)
end
local function getStackHeight(position, room)
local height = 0
for _, pole in ipairs(room:GetChildren()) do
if pole:IsA("BasePart") then
local polePos = pole.Position
if math.abs(polePos.X - position.X) < 0.1 and math.abs(polePos.Z - position.Z) < 0.1 then
height = math.max(height, polePos.Y + pole.Size.X)
end
end
end
return height
end
local function isWithinAssignedRoom(player, position)
local roomName = player:GetAttribute("Room")
if not roomName then
warn("Player has no room attribute.")
return false
end
local roomPart = workspace.PlayerRooms:FindFirstChild(roomName)
if not roomPart or not roomPart:IsA("BasePart") then
warn("Room part not found or invalid for player:", player.Name)
return false
end
local relativePosition = roomPart.CFrame:PointToObjectSpace(position)
local size = roomPart.Size
local withinX = math.abs(relativePosition.X) <= size.X / 2
local withinY = math.abs(relativePosition.Y) <= size.Y / 2 + 10 -- Allow a bit of vertical buffer
local withinZ = math.abs(relativePosition.Z) <= size.Z / 2
return withinX and withinY and withinZ
end
placeRequest.OnServerEvent:Connect(function(player, position)
print("Received place request from player:", player.Name)
print("Requested position:", position)
if isWithinAssignedRoom(player, position) then
print("Position is within player's assigned room.")
local placedPole = tool.Handle:Clone()
placedPole.Anchored = true
placedPole.CanCollide = true
local snappedPosition = snapToGrid(position, GRID_SIZE)
print("Snapped position:", snappedPosition)
local playerRoomName = player:GetAttribute("Room")
local playerRoom = workspace.PlayerRooms[playerRoomName]
print("Player room identified:", playerRoomName)
local currentStackHeight = getStackHeight(snappedPosition, playerRoom)
print("Calculated current stack height:", currentStackHeight)
local poleHeight = placedPole.Size.X
local finalY = currentStackHeight + (poleHeight / 2)
print("Final pole Y-position:", finalY)
placedPole.CFrame = CFrame.new(snappedPosition.X, finalY, snappedPosition.Z)
* CFrame.Angles(0, 0, math.rad(90))
placedPole.Parent = playerRoom
print("Pole placed successfully at:", placedPole.Position)
tool:Destroy()
print("Pole tool removed from player.")
else
warn(player.Name .. " tried placing outside their assigned room!")
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment