Resizing part on one face

Hello, I’m currently making a tree cutting system but I’m having trouble resizing a part after the tree is cut.

The player attacks a tree, and if their mouse is on a part and the hitbox detects a tree the script will create a new part with its length being the difference between the location of the mouse on the original part and the length of the original part.

Below is the script, and a screenshot:



The newly created part’s position ends up going too high on its UpVector and I’m having trouble finding out why that is. Any help will be appreciated!

2 Likes

Try using this instead, it’ll determine the exact position of where the cut occurs based on the player’s mouse position, and ensure that the new part is positioned correctly using the calculated cut position which adjusts for the height of the original part

local function onTreeCut(originalPart, mousePosition)
    -- Get the original part's size and position
    local originalSize = originalPart.Size
    local originalPosition = originalPart.Position

    -- Calculate the cut position based on the mouse's position
    local cutHeight = (originalPosition.Y - mousePosition.Y) -- Difference in height
    local newSize = Vector3.new(originalSize.X, originalSize.Y - cutHeight, originalSize.Z)

    -- Create the new part
    local newPart = Instance.new("Part")
    newPart.Size = newSize
    newPart.Position = originalPosition - Vector3.new(0, cutHeight / 2, 0) -- Adjust position
    newPart.Parent = workspace

    -- Optionally set newPart's Anchored, CanCollide, etc.
    newPart.Anchored = true
    newPart.CanCollide = true
end

So the part ends up looking like this:

This is the code in case I did something wrong

Also, is this a viable way to get the mouse position on the part that gets clicked? I have a feeling this may be part of the problem.

image

1 Like

Oh my apologies try this instead

local function onTreeCut(originalPart, mouse)
    -- Get the original part's size and position
    local originalSize = originalPart.Size
    local originalPosition = originalPart.Position

    -- Calculate the cut position based on the mouse's position
    local mousePosition = mouse.Hit.p
    local cutHeight = originalPosition.Y - mousePosition.Y -- Difference in height
    local newSize = Vector3.new(originalSize.X, originalSize.Y - cutHeight, originalSize.Z)

    -- Create the new part
    local newPart = Instance.new("Part")
    newPart.Size = newSize
    newPart.Position = originalPosition - Vector3.new(0, cutHeight / 2, 0) -- Adjust position
    newPart.Parent = workspace

    -- Optionally set newPart's Anchored, CanCollide, etc.
    newPart.Anchored = true
    newPart.CanCollide = true
end

Also make sure to pass the mouse object when calling onTreeCut, and ensure that your mouse event is properly set up to call this function, and if you do still end up have issues you should double check the logic for getting the mouse position and ensure that it aligns with the part’s coordinate space

1 Like

Solution found, in my original code I just needed to subtract the new CFrame by the original part’s size Y value and divide it by 2. Thank you for your assistance!

2 Likes

That’s great, goodluck on your game!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.