Skip to content

Instantly share code, notes, and snippets.

@MajorTal
Created June 3, 2025 19:15
Show Gist options
  • Save MajorTal/bd439a52ecdf1674f4fb31e2f4874b94 to your computer and use it in GitHub Desktop.
Save MajorTal/bd439a52ecdf1674f4fb31e2f4874b94 to your computer and use it in GitHub Desktop.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local ShopItems = require(ServerStorage.ShopItems)
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 isWithinAssignedRoom(player, position)
local roomName = player:GetAttribute("Room")
local room = workspace.PlayerRooms:FindFirstChild(roomName)
if not room then return false end
local relativePos = room.CFrame:PointToObjectSpace(position)
local size = room.Size
return math.abs(relativePos.X) <= size.X/2 and math.abs(relativePos.Z) <= size.Z/2
end
-- Listen globally for all placement requests
local placementEvent = ReplicatedStorage:WaitForChild("GlobalPlaceRequest")
placementEvent.OnServerEvent:Connect(function(player, itemType, position)
local itemDef = ShopItems[itemType]
if not itemDef then
warn("Unknown item requested:", itemType)
return
end
local cash = player.leaderstats.Cash
if cash.Value < itemDef.Price then
warn("Not enough cash:", player.Name)
return
end
if isWithinAssignedRoom(player, position) then
local modelRef = ServerStorage:FindFirstChild(itemDef.Model)
if not modelRef then
warn("Model not found:", itemDef.Model)
return
end
local placedItem = modelRef.Handle:Clone()
placedItem.Anchored = true
placedItem.CanCollide = true
local snappedPosition = snapToGrid(position, 4)
local room = workspace.PlayerRooms[player:GetAttribute("Room")]
local roomTopSurfaceY = room.Position.Y + (room.Size.Y / 2)
-- Adjust Y based on item's height
local itemHeight = placedItem.Size.Y
local finalY = roomTopSurfaceY + (itemHeight / 2)
placedItem.CFrame = CFrame.new(snappedPosition.X, finalY, snappedPosition.Z)
placedItem.Parent = room
cash.Value -= itemDef.Price
-- Immediately save data
-- (Call your existing save function here if necessary)
else
warn(player.Name.." tried placing outside their room")
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment