Trail to follow and only attach to morph

So I am trying to create a game were you have to select a morph and each morph would give you a colored line that colored morph. It works on the normal character but I have now been trying to only attach it to the Morph itself that when you become that morph it would work with that specific morph.

This is the code I have:

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

local function createRedPart(position)
    local part = Instance.new("Part")
    part.Size = Vector3.new(1, 0.2, 1) -- Thin part for smooth texture
    part.Color = Color3.new(1, 0, 0) -- Red color
    part.Anchored = true
    part.CanCollide = false
    part.Material = Enum.Material.SmoothPlastic -- Smooth texture
    part.Position = position - Vector3.new(0, 3, 0) -- Position below the legs
    part.Parent = workspace
end

local function onCharacterAdded(character)
    local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
    local lastPosition = humanoidRootPart.Position

    RunService.RenderStepped:Connect(function()
        local currentPosition = humanoidRootPart.Position
        if (currentPosition - lastPosition).magnitude > 1 then
            createRedPart(lastPosition)
            lastPosition = currentPosition
        end
    end)
end

local function onPlayerAdded(player)
    local character = player.Character or player.CharacterAdded:Wait()
    onCharacterAdded(character)
    player.CharacterAdded:Connect(onCharacterAdded)
end

-- Bind the logic to incoming players
Players.PlayerAdded:Connect(onPlayerAdded)

-- Iterate through all existing players and attach the logic
for _, player in Players:GetPlayers() do
    onPlayerAdded(player)
end

I think my main problem is cause it is set to StarterPlayer and not the specific model but I do not know how to set it to a specific model even when you morph into it. I am a beginner scripter so I have been trying my best but do not know what to do from here.

The video shows an example as to what I am talking about.
robloxapp-20240831-1542323.wmv (2.2 MB)

1 Like

could you provide the morph model you are using in your game?

i modified your script to do what i think you were trying to achieve:

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

local function createRedPart(position)
	local part = Instance.new("Part")
	part.Size = Vector3.new(1, 0.2, 1) -- Thin part for smooth texture
	part.Color = Color3.new(1, 0, 0) -- Red color
	part.Anchored = true
	part.CanCollide = false
	part.Material = Enum.Material.SmoothPlastic -- Smooth texture
	part.Position = position
	part.Parent = workspace
end

local function onCharacterAdded(character)
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	local lastPosition = humanoidRootPart.Position

	RunService.RenderStepped:Connect(function()		
		local rayOrigin = humanoidRootPart.Position
		local rayDirection = Vector3.new(0, -4, 0) -- downward direction from humanoidrootpart position
		
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {character:GetDescendants()} -- filter out character limbs
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude

		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams) -- create raycast
		
		if raycastResult then -- if ray makes contact, create red part
			local currentPosition = raycastResult.Position -- position where ray makes contact with ground
			
			if (currentPosition - lastPosition).magnitude > 1 then
				createRedPart(lastPosition)
				lastPosition = currentPosition
			end
		end
	end)
end

local function onPlayerAdded(player)
	local character = player.Character or player.CharacterAdded:Wait()
	onCharacterAdded(character)
	player.CharacterAdded:Connect(onCharacterAdded)
end

-- Bind the logic to incoming players
Players.PlayerAdded:Connect(onPlayerAdded)

-- Iterate through all existing players and attach the logic
for _, player in Players:GetPlayers() do
	onPlayerAdded(player)
end

instead of creating a red part a few studs underneath the humanoidrootpart, i created a raycast that makes contact with the ground and then creates a red part at that contact position. i assume you wanted to create red parts only when the player is touching the ground but this can be modified to create red parts without contact too.

There is an instance ‘Trail’ at your disposal, so you do not need to constantly create red parts
It does need 2 attachments tho so place them in your morphs where you want the trail to stretch from. The docs on it here if you need more help.