Trail For a Snail Cframe and Raycasting

So basically I want to make a trail that follows a snail. I want the trail to be a part. The trail needs to be on the floor when it spawns so that’s where raycasting comes into play. I also need it to check if it’s able to make a new trail part when the distance is appropriate.

The problem is that I don’t know where to start. I have the NPC ready. I have a picture on what it should look like if it was working.

You will need to list the points at which the start of a new part of the trail will be created
Then check to see it a certain minimum distance has been traveled
Then create a part and add it to the list
Remove old parts in the list

^ idea of how I might start with it

I put what I wrote into chatgpt and here is a code example:

local snailNPC = game.Workspace.Snail -- Adjust to the correct reference for your snail NPC
local trailParts = {} -- List to store the trail parts
local trailDistanceThreshold = 5 -- Minimum distance to create a new trail part
local maxTrailParts = 20 -- Maximum number of trail parts to keep (optional)

local function createTrailPart(position)
    local trailPart = Instance.new("Part")
    trailPart.Size = Vector3.new(1, 0.1, 1) -- Flat part for the trail
    trailPart.Position = position
    trailPart.Anchored = true
    trailPart.CanCollide = false
    trailPart.Parent = workspace
    table.insert(trailParts, trailPart)
    
    -- Remove old parts if we exceed the max number of trail parts
    if #trailParts > maxTrailParts then
        local oldPart = table.remove(trailParts, 1)
        oldPart:Destroy() -- Remove the oldest trail part
    end
end

local lastPosition = snailNPC.HumanoidRootPart.Position

-- Function to check if the snail has moved enough to add a new trail part
local function checkForNewTrailPart()
    local currentPosition = snailNPC.HumanoidRootPart.Position
    local distance = (currentPosition - lastPosition).Magnitude

    if distance >= trailDistanceThreshold then
        -- Raycast to find the floor position to place the trail part
        local direction = Vector3.new(0, -10, 0) -- Raycast downward
        local ray = workspace:Raycast(currentPosition, direction)
        
        if ray then
            local spawnPosition = ray.Position
            createTrailPart(spawnPosition)
        end
        
        -- Update last position to current
        lastPosition = currentPosition
    end
end

-- Update continuously
game:GetService("RunService").Heartbeat:Connect(function()
    checkForNewTrailPart()
end)

I also thought of that Idea. I placed an attachment on one side of a trail and was planning to measure from there. It would give me the position and exact rotation, although I haven’t thought of how the raycasting would work.

can you not raycast in the negative y position/direction? That would give you the floor.

How would I be able to reenact it?