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.
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
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
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!