Pathfinding Service

  1. What do you want to achieve? I want to make a music note go into a player’s torso when they press on their screen. Once the note goes to the torso it will destroy itself. Not sure why my script is erroring. I basically want the pathfinding service to make the object go in this path.

  1. What is the issue? The 1st issue is the music note isn’t going where I want it to go, it just falls under the player (the part is anchored). 2nd is I’m not really sure how to get the music note into the motion I want it to. I added a part that indicates the endpoint (which is transparent behind the head of the player) of the music note. That part also falls. Here’s my code.
local DataStoreV2 = require(1936396537)
local Player = script.Parent.Parent.Parent
local Character = Player.Character
local MusicDS = DataStoreV2("MusicDataStore2", Player)
local TotalMusicDS = DataStoreV2("TotalMusicDataStore2", Player)
local AlbumDS = DataStoreV2("AlbumDataStore2", Player)
local Debounce = false
local DesiredAmount = 5
local MusicNote = game.ReplicatedStorage.Objects.MusicNoteSystem.MusicNote:Clone()
local endPoint = game.ReplicatedStorage.Objects.MusicNoteSystem.endPoint:Clone()

local PathParts = Instance.new("Model", Head)
PathParts.Name = "PathParts"

local function ShowMusicNote()
	local Head = Character.Head
	
	MusicNote.Position = Vector3.new(Head.Position.X, Head.Position.Y, Head.Position.Z - 10)
	MusicNote.Parent = Head
	
	endPoint.Parent = Head
	endPoint.Position = Vector3.new(Head.Position)
	
	local PathFindingService = game:GetService("PathfindingService")
	local Path = PathFindingService:CreatePath()
	
	Path:ComputeAsync(MusicNote.Position, endPoint.Position)
	
	local Waypoints = Path:GetWaypoints()
	
		for i = 1,5 do
			if i < #Waypoints then
			local Tracker = Instance.new("Part")
			Tracker.Anchored = true
			Tracker.Size = Vector3.new(0.5,0.5,0.5)
			Tracker.Position = Waypoints[i].Position
			Tracker.CanCollide = false
			Tracker.Transparency = 1
			MusicNote.Position = Waypoints[i].Position
			wait(.1)
		end
	end
end

--Everything from here down works!

script.Parent.Activated:Connect(function()
	if Debounce == false then
		if Player.PlayerValues.Music.Value < Player.PlayerValues.Album.Value then
			Debounce = true
			-- TotalMusicDS:Increment(DesiredAmount, 0)
			-- MusicDS:Increment(DesiredAmount, 0)
			ShowMusicNote()
			wait(2)
			Debounce = false
		end
	end
end)

If anyone knows the issue or a solution please let me know!

Pathfinding service is mainly used for moving NPCs towards an object. From the way you seem to have this set up (and what you’re trying to achieve), I’d guess using TweenService would be better.

I would make this whole effect one function where the pseudo-code looks something along these lines:

function NoteEffect()
    -- Clone a copy of the note and position it in front of the characters head
    -- Depending on how you want the note to move, tween it to the characters head (using TweenService)
    -- Once the tween is complete (Tween.Completed event), destroy the note you cloned
end

Depending on if you want this effect replicated or not, you would have to pass in the player (or character) as the first argument for who to do the effect on. If it’s 100% client sided, you can just use the LocalPlayer.

1 Like

Hey, the part still falls to the floor, here’s the new script I made.

local function ShowMusicNote()
	local Head = Character.Head
	local Torso = Character.UpperTorso
	
	local MusicNote = game.ReplicatedStorage.Objects.MusicNoteSystem.MusicNote:Clone()
	MusicNote.Parent = Head
	MusicNote.Position = Vector3.new(Head.Position.X, Head.Position.Y, Head.Position.Z -10)
	
	local TweenService = game:GetService("TweenService")
	
	local Tween1 = TweenService:Create(MusicNote, TweenInfo.new(2), {Position = Vector3.new(Head.Position.X, Head.Position.Y +5, Head.Position.Z)})
	local Tween2 = TweenService:Create(MusicNote, TweenInfo.new(2), {Position = Vector3.new(Torso.Position)})
	
	Tween1:Play()
     wait(2)
	Tween2:Play()
end

Nevermind, just fixed it. Thanks for your help.

Actually, I have a new question lol, how would I make it so if the player moves the music note will follow with it. Because now when the player moves the music note does the tween but in the place where the Character.Head was before. Here’s what the script looks like.

local function ShowMusicNote()
		local Head = Character.Head
		local Torso = Character.UpperTorso
		
		local MusicNote = game.ReplicatedStorage.Objects.MusicNoteSystem.MusicNote:Clone()
		MusicNote.Parent = Head
		MusicNote.Position = Vector3.new(Head.Position.X, Head.Position.Y, Head.Position.Z -5)
		MusicNote.Anchored = true
		
		wait()
		local TweenService = game:GetService("TweenService")
		local Tween1 = TweenService:Create(MusicNote, TweenInfo.new(2), {Position = Vector3.new(Head.Position.X, Head.Position.Y +3, Head.Position.Z)})
		local Tween2 = TweenService:Create(MusicNote, TweenInfo.new(2), {Position = Vector3.new(Torso.Position)})
		
		Tween1:Play()
		wait(1)
		Tween2:Play()
		wait(1)
		MusicNote:Destroy()
end