#model placement system not taking into account bigger models

10 messages · Page 1 of 1 (latest)

winged torrent
#

local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local pickupDistance = 5 -- How close the player must be to pick up the object
local holdingItem = nil -- Stores the currently held item
local holdingWeld = nil -- Stores the weld constraint

-- Function to find the closest object
local function findClosestItem(player)
local character = player.Character
if not character then return end

local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end

local closestItem = nil
local minDistance = pickupDistance

for _, item in pairs(workspace:GetChildren()) do
    if item:IsA("Model") and item.PrimaryPart and item:FindFirstChild("CanBePickedUp") then
        local distance = (humanoidRootPart.Position - item.PrimaryPart.Position).Magnitude
        if distance < minDistance then
            closestItem = item
            minDistance = distance
        end
    end
end

return closestItem

end

-- Function to play a unique sound for each model
local function playSound(item)
local sound = item:FindFirstChild("PickupSound")
if sound and sound:IsA("Sound") then
sound:Play()
end
end

-- Function to pick up an item
local function pickUpItem(player, item)
if not item or holdingItem then return end

local character = player.Character
if not character then return end

local rightHand = character:FindFirstChild("RightHand") or character:FindFirstChild("Right Arm")
if not rightHand then return end

item.PrimaryPart.Anchored = false
item.Parent = character
holdingItem = item

holdingWeld = Instance.new("Motor6D")
holdingWeld.Part0 = rightHand
holdingWeld.Part1 = item.PrimaryPart
holdingWeld.C0 = CFrame.new(0, 0, -1) -- Position the item in front of the hand
holdingWeld.Parent = rightHand

playSound(item)

end

#

-- Function to drop an item
local function dropItem(player)
if not holdingItem then return end

local character = player.Character
if not character then return end

if holdingWeld then
    holdingWeld:Destroy()
    holdingWeld = nil
end

local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end

holdingItem.PrimaryPart.Anchored = true
holdingItem:SetPrimaryPartCFrame(humanoidRootPart.CFrame * CFrame.new(0, -2, -2) * CFrame.Angles(0, math.rad(0), 0)) -- Places the item upright in front of the player
holdingItem.Parent = workspace

local sound = holdingItem:FindFirstChild("DropSound")
if sound and sound:IsA("Sound") then
    sound:Play()
end

holdingItem = nil

end

-- Detect key press
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end

if input.KeyCode == Enum.KeyCode.E then
    local player = game.Players.LocalPlayer

    if holdingItem then
        dropItem(player)
    else
        local item = findClosestItem(player)
        if item then
            pickUpItem(player, item)
        end
    end
end

end)

deep ember
# winged torrent -- Function to drop an item local function dropItem(player) if not holdingIt...

hi, the reason is because your system is using a fixed offset (CFrame.new(0, 0, -1)) when attaching the object to the players hand which doesn't account for different model sizes If a model is larger it might clip into the players hand or be positioned awkwardly

to fix this ~~~

replace this:

holdingWeld.C0 = CFrame.new(0, 0, -1)```
### with:
```lua
local size = item:GetExtentsSize()
holdingWeld.C0 = CFrame.new(0, 0, -(size.Z / 2 + 0.5))```
winged torrent
#

Thank you so much!

deep ember
winged torrent
#

I will! I'm just about to test it! :D

deep ember
#

okayy hopefully

winged torrent
#

It worked ty!! :D

deep ember