Advertisement
BADABATS

Server Placement

May 20th, 2025
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. -- Server Placement
  2.  
  3. local DataHandler = require(game.ReplicatedStorage.Modules.DataHandler)
  4.  
  5. -- Handles collisions on the hitbox
  6. local function handleCollisions(object, char, plotModel)
  7. local collided = false
  8.  
  9. if object then
  10. local collisionPoint = object.PrimaryPart.Touched:Connect(function() end)
  11. local collisionPoints = object.PrimaryPart:GetTouchingParts()
  12.  
  13. for i = 1, #collisionPoints do
  14. if not collisionPoints[i]:IsDescendantOf(object) and not collisionPoints[i]:IsDescendantOf(plotModel) then
  15. collided = true
  16. break
  17. end
  18. end
  19.  
  20. collisionPoint:Disconnect()
  21. end
  22.  
  23. return collided
  24. end
  25.  
  26. local function checkBoundaries(plot, primary, relativeCFrame)
  27. -- Use the relative position directly from relativeCFrame
  28. local relativePosition = relativeCFrame.Position
  29.  
  30. -- Define plot boundaries in terms of its size and relative coordinates
  31. local halfPlotX = plot.Size.X * 0.5
  32. local halfPlotZ = plot.Size.Z * 0.5
  33.  
  34. local LOWER_X_BOUND = -halfPlotX
  35. local UPPER_X_BOUND = halfPlotX
  36. local LOWER_Z_BOUND = -halfPlotZ
  37. local UPPER_Z_BOUND = halfPlotZ
  38.  
  39. -- Check if the relative position is within the plot's boundaries
  40. local outOfBounds =
  41. relativePosition.X < LOWER_X_BOUND or relativePosition.X > UPPER_X_BOUND or
  42. relativePosition.Z < LOWER_Z_BOUND or relativePosition.Z > UPPER_Z_BOUND
  43.  
  44. if outOfBounds then
  45. print("Relative Position:", relativePosition)
  46. print("Bounds:", LOWER_X_BOUND, UPPER_X_BOUND, LOWER_Z_BOUND, UPPER_Z_BOUND)
  47. print("Out of bounds detected")
  48. end
  49.  
  50. return outOfBounds
  51. end
  52.  
  53. local function getNextItemNumber(id, loc)
  54. local maxNumber = 0
  55. local existingNumbers = {}
  56.  
  57. -- Gather all existing numbers for this id
  58. for _, existingItem in ipairs(loc:GetChildren()) do
  59. if existingItem.Name:match("^" .. id .. "%d+$") then
  60. local num = tonumber(existingItem.Name:match("%d+$"))
  61. if num then
  62. existingNumbers[num] = true
  63. if num > maxNumber then
  64. maxNumber = num
  65. end
  66. end
  67. end
  68. end
  69.  
  70. -- Find the lowest available number that isn't in use
  71. for i = 1, maxNumber do
  72. if not existingNumbers[i] then
  73. return i
  74. end
  75. end
  76.  
  77. -- If no gaps, return the next highest number
  78. return maxNumber + 1
  79. end
  80.  
  81. -- Update in place function
  82. local function place(plr, id, loc, cf, plot, plotmodel)
  83. -- Get the next available item number
  84. local nextNumber = getNextItemNumber(id, loc)
  85.  
  86. -- Find and clone the item
  87. local item = game.ReplicatedStorage.GrowPlace.GrowItems:FindFirstChild(id):Clone()
  88. item.PrimaryPart.CanCollide = false
  89. item.PrimaryPart.Transparency = 1
  90.  
  91. -- Set the item's name with the next available number
  92. item.Name = id .. tostring(nextNumber)
  93.  
  94. -- Place item in location if plot is valid
  95. if plot then
  96. item.Parent = loc
  97.  
  98. -- Convert cf back to world position to verify boundary check
  99. local worldCFrame = plot.CFrame * cf
  100. item:PivotTo(worldCFrame)
  101.  
  102. -- Check boundaries and collisions
  103. if checkBoundaries(plot, item.PrimaryPart, cf) then
  104. print("Out of bounds")
  105. item:Destroy()
  106. return false
  107. end
  108.  
  109. if handleCollisions(item, plr.Character, plotmodel) then
  110. item:Destroy()
  111. return false
  112. end
  113. end
  114.  
  115. -- Update player inventory and add item to GrowPlaceHandler
  116. if DataHandler.PlayerInventoryHandler(plr, id, 1, true) then
  117. DataHandler.GrowPlaceHandler(plr, item, cf, true)
  118. return true
  119. else
  120. item:Destroy()
  121. return false
  122. end
  123. end
  124.  
  125. local function removeItem(plr, machineModel, plot)
  126. local growItemHolder
  127. local maxRetries = 5
  128. local retries = 0
  129.  
  130. -- Retry mechanism for finding GrowItemHolder
  131. while retries < maxRetries do
  132. growItemHolder = plot and plot:FindFirstChild("GrowItemHolder")
  133. if growItemHolder then break end
  134. retries += 1
  135. warn("retrying")
  136. wait(0.1) -- Small delay before retrying
  137. end
  138.  
  139. if not growItemHolder then
  140. warn("Failed to locate GrowItemHolder after retries.")
  141. return false
  142. end
  143.  
  144. -- Check if machineModel exists and is part of GrowItemHolder
  145. if machineModel and machineModel:IsDescendantOf(growItemHolder) then
  146. -- Strip the number suffix to get the base item name
  147. local itemName = machineModel.Name:match("^(%a+)")
  148. if not itemName then
  149. warn("Failed to identify base item name.")
  150. return false
  151. end
  152.  
  153. -- Return the item to the player's inventory
  154. local success = DataHandler.GrowPlaceHandler(plr, machineModel, nil, nil, true)
  155. if success then
  156. print("Item returned to inventory.")
  157. -- Destroy the model on the plot
  158. machineModel:Destroy()
  159. return true
  160. else
  161. warn("Failed to return item to inventory.")
  162. return false
  163. end
  164. else
  165. print(machineModel)
  166. warn("Machine model is invalid or not part of GrowItemHolder.")
  167. return false
  168. end
  169. end
  170.  
  171.  
  172.  
  173.  
  174.  
  175. -- Bind the function to the RemoteFunction
  176. game.ReplicatedStorage.Events.RemoteFunctions.removeItem.OnServerInvoke = removeItem
  177. game.ReplicatedStorage.Events.RemoteFunctions.place.OnServerInvoke = place
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement