Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server Placement
- local DataHandler = require(game.ReplicatedStorage.Modules.DataHandler)
- -- Handles collisions on the hitbox
- local function handleCollisions(object, char, plotModel)
- local collided = false
- if object then
- local collisionPoint = object.PrimaryPart.Touched:Connect(function() end)
- local collisionPoints = object.PrimaryPart:GetTouchingParts()
- for i = 1, #collisionPoints do
- if not collisionPoints[i]:IsDescendantOf(object) and not collisionPoints[i]:IsDescendantOf(plotModel) then
- collided = true
- break
- end
- end
- collisionPoint:Disconnect()
- end
- return collided
- end
- local function checkBoundaries(plot, primary, relativeCFrame)
- -- Use the relative position directly from relativeCFrame
- local relativePosition = relativeCFrame.Position
- -- Define plot boundaries in terms of its size and relative coordinates
- local halfPlotX = plot.Size.X * 0.5
- local halfPlotZ = plot.Size.Z * 0.5
- local LOWER_X_BOUND = -halfPlotX
- local UPPER_X_BOUND = halfPlotX
- local LOWER_Z_BOUND = -halfPlotZ
- local UPPER_Z_BOUND = halfPlotZ
- -- Check if the relative position is within the plot's boundaries
- local outOfBounds =
- relativePosition.X < LOWER_X_BOUND or relativePosition.X > UPPER_X_BOUND or
- relativePosition.Z < LOWER_Z_BOUND or relativePosition.Z > UPPER_Z_BOUND
- if outOfBounds then
- print("Relative Position:", relativePosition)
- print("Bounds:", LOWER_X_BOUND, UPPER_X_BOUND, LOWER_Z_BOUND, UPPER_Z_BOUND)
- print("Out of bounds detected")
- end
- return outOfBounds
- end
- local function getNextItemNumber(id, loc)
- local maxNumber = 0
- local existingNumbers = {}
- -- Gather all existing numbers for this id
- for _, existingItem in ipairs(loc:GetChildren()) do
- if existingItem.Name:match("^" .. id .. "%d+$") then
- local num = tonumber(existingItem.Name:match("%d+$"))
- if num then
- existingNumbers[num] = true
- if num > maxNumber then
- maxNumber = num
- end
- end
- end
- end
- -- Find the lowest available number that isn't in use
- for i = 1, maxNumber do
- if not existingNumbers[i] then
- return i
- end
- end
- -- If no gaps, return the next highest number
- return maxNumber + 1
- end
- -- Update in place function
- local function place(plr, id, loc, cf, plot, plotmodel)
- -- Get the next available item number
- local nextNumber = getNextItemNumber(id, loc)
- -- Find and clone the item
- local item = game.ReplicatedStorage.GrowPlace.GrowItems:FindFirstChild(id):Clone()
- item.PrimaryPart.CanCollide = false
- item.PrimaryPart.Transparency = 1
- -- Set the item's name with the next available number
- item.Name = id .. tostring(nextNumber)
- -- Place item in location if plot is valid
- if plot then
- item.Parent = loc
- -- Convert cf back to world position to verify boundary check
- local worldCFrame = plot.CFrame * cf
- item:PivotTo(worldCFrame)
- -- Check boundaries and collisions
- if checkBoundaries(plot, item.PrimaryPart, cf) then
- print("Out of bounds")
- item:Destroy()
- return false
- end
- if handleCollisions(item, plr.Character, plotmodel) then
- item:Destroy()
- return false
- end
- end
- -- Update player inventory and add item to GrowPlaceHandler
- if DataHandler.PlayerInventoryHandler(plr, id, 1, true) then
- DataHandler.GrowPlaceHandler(plr, item, cf, true)
- return true
- else
- item:Destroy()
- return false
- end
- end
- local function removeItem(plr, machineModel, plot)
- local growItemHolder
- local maxRetries = 5
- local retries = 0
- -- Retry mechanism for finding GrowItemHolder
- while retries < maxRetries do
- growItemHolder = plot and plot:FindFirstChild("GrowItemHolder")
- if growItemHolder then break end
- retries += 1
- warn("retrying")
- wait(0.1) -- Small delay before retrying
- end
- if not growItemHolder then
- warn("Failed to locate GrowItemHolder after retries.")
- return false
- end
- -- Check if machineModel exists and is part of GrowItemHolder
- if machineModel and machineModel:IsDescendantOf(growItemHolder) then
- -- Strip the number suffix to get the base item name
- local itemName = machineModel.Name:match("^(%a+)")
- if not itemName then
- warn("Failed to identify base item name.")
- return false
- end
- -- Return the item to the player's inventory
- local success = DataHandler.GrowPlaceHandler(plr, machineModel, nil, nil, true)
- if success then
- print("Item returned to inventory.")
- -- Destroy the model on the plot
- machineModel:Destroy()
- return true
- else
- warn("Failed to return item to inventory.")
- return false
- end
- else
- print(machineModel)
- warn("Machine model is invalid or not part of GrowItemHolder.")
- return false
- end
- end
- -- Bind the function to the RemoteFunction
- game.ReplicatedStorage.Events.RemoteFunctions.removeItem.OnServerInvoke = removeItem
- game.ReplicatedStorage.Events.RemoteFunctions.place.OnServerInvoke = place
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement