How to make an arrow point at a part?

Hello!

I am trying to make an arrow that points to a part. I’ve made a local script but it doesn’t work.

local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local part =  game:GetService("Workspace").OminusOfficesMap.Dumpster1.DumpsterOpenMain:WaitForChild("Main")
local arrow = game:GetService("ReplicatedStorage").Arrow.Arrow:Clone()
arrow.Parent = player

local offset = Vector3.new(10,1,0)

player.CharacterAdded:Connect(function(character)
	runService.HeartBeat:Connect(function()
		if character and character:FindFirstChild("Humanoid") and character.Humanoid.Health ~= 0 then
			arrow.CFrame = CFrame.new((Camera.CFrame + offset).position, part.Position) 
		else
			arrow.Position = Vector3.new(0,-10,0) -- hide the arrow
			return -- if im right this should stop the loop, otherwise if you'd reset you'd still have this running and another one, the valid one, else you should remove this line.
		end
	end)
end)

I don’t think the arrow will render if it’s parented to the Player: it has to be under the workspace.

Also, are you sure you want it at the camera CFrame with some offset? You might want to center it around the character unless perhaps if it’s in first person.

I moved the arrow to workspace, and made it zero offset. But the arrow doesn’t stick to the player’s position.

local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local part =  game:GetService("Workspace").OminusOfficesMap.Dumpster1.DumpsterOpenMain:WaitForChild("Main")
local arrow = game:GetService("ReplicatedStorage").Arrow.Arrow:Clone()
arrow.Parent = game:GetService("Workspace")

local offset = Vector3.new(0,0,0)

player.CharacterAdded:Connect(function(character)
	runService.HeartBeat:Connect(function()
		if character and character:FindFirstChild("Humanoid") and character.Humanoid.Health ~= 0 then
			arrow.CFrame = CFrame.new((Camera.CFrame + offset).position, part.Position) 
		else
			arrow.Position = Vector3.new(0,-10,0) -- hide the arrow
			return -- if im right this should stop the loop, otherwise if you'd reset you'd still have this running and another one, the valid one, else you should remove this line.
		end
	end)
end)

Does the arrow just stay in one place? You can check where the arrow is in the explorer.

Currently (if it’s working as expected) it would intersect with the camera, so you probably want to center it around the character’s HumanoidRootPart’s position. Otherwise idk if you could see the arrow because the camera would be inside of it.

The arrow stays at one position

The how do you centre it around the character’s HumanoidRootPart’s position?

Here is an example place with the working code:

DevForumArrowExample.rbxl (55.8 KB)

examplearrow

Here are the fixes:

  • The main one: if a character already exists when you create the player.CharacterAdded connection, it never starts the Heartbeat connection for that character. Basically, CharacterAdded is triggered before you start the connection for it, so it doesn’t catch the first character.
  • runService.HeartBeat to runService.Heartbeat
  • Added code to end the connection when the character is destroyed
  • Waited for the instances to be added to ReplicatedStorage and the Workspace
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local part = Workspace:WaitForChild("OminusOfficesMap"):WaitForChild("Dumpster1"):WaitForChild("DumpsterOpenMain"):WaitForChild("Main")
	
local arrow = ReplicatedStorage:WaitForChild("Arrow"):WaitForChild("Arrow"):Clone()
arrow.Parent = Workspace

local offset = Vector3.new(0,5,0)

local function setUpCharacter(character)
	while not character:IsDescendantOf(Workspace) do
		task.wait()
	end
	
	local connection
	connection = RunService.Heartbeat:Connect(function()
		-- If the character was destroyed and is no longer in Workspace, stop the Heartbeat connection and end the function
		if not character:IsDescendantOf(Workspace) then
			connection:Disconnect()
			return
		end
		
		if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") and character.Humanoid.Health > 0 then
			arrow.CFrame = CFrame.new(character.HumanoidRootPart.Position + offset, part.Position)
		else
			arrow.Position = Vector3.new(0,-10,0) -- hide the arrow
			return -- This doesn't stop the loop. You can stop the loop by using connection:Disconnect() and storing the connection like above
		end
	end)
end

if player.Character then
	setUpCharacter(player.Character)
end

player.CharacterAdded:Connect(setUpCharacter)

If you’re using StreamingEnabled (it’s now the default), you might run into a problem where the target part is streamed out. To fix this, make sure the target part is in a model set to Persistent.

I got this error:
17:02:17.865 CFrame is not a valid member of Folder “Workspace.Arrow” - Client - ObjectiveHandler:29

it might be because the arrow is a union, but I don’t know.

hey man, tip for u, instead of typing all this:

local part =  game:GetService("Workspace").OminusOfficesMap.Dumpster1.DumpsterOpenMain:WaitForChild("Main")

u can do this:

local map = workspace.OminusOfficesMap
-- the second parameter in :FindFirstChild(); recursive, allows u to search anywhere within an instance (folder, model, etc) for a specific item. :-)
local part = map:FindFirstChild("Main", true)

for the arrow itself,
u can do something like this:

arrow.CFrame = CFrame.lookat((Camera.CFrame + offset).Position, part.Position) 

i would advise u to leave the heartbeat event outside of the characteradded event bc it’ll make a new connection every time a player respawns which could inevitably break your arrow.

	runService.HeartBeat:Connect(function()
		if character and character:FindFirstChild("Humanoid") and character.Humanoid.Health ~= 0 then
			arrow.CFrame = CFrame.new((Camera.CFrame + offset).position, part.Position) 
		else
			arrow.Position = Vector3.new(0,-10,0) -- hide the arrow
			return -- if im right this should stop the loop, otherwise if you'd reset you'd still have this running and another one, the valid one, else you should remove this line.
		end
	end)

player.CharacterAdded:Connect(function(character)

end)

hopefully this helps!

A union is fine. That error is because ReplicatedStorage.Arrow.Arrow is a Folder instead of a Union or a Part. Try changing this line so it matches where your arrow union is stored:

local arrow = ReplicatedStorage:WaitForChild("Arrow"):WaitForChild("Arrow"):Clone()

It works but where the arrow is pointing is a bit off.

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

local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local map = workspace.OminusOfficesMap:WaitForChild("Dumpster3")
-- the second parameter in :FindFirstChild(); recursive, allows u to search anywhere within an instance (folder, model, etc) for a specific item. :-)
local part = map:FindFirstChild("Main", true)

local arrow = ReplicatedStorage:WaitForChild("Arrow"):WaitForChild("Arrow"):Clone()
arrow.Parent = Workspace

local offset = Vector3.new(0,5,0)

local function setUpCharacter(character)
	while not character:IsDescendantOf(Workspace) do
		task.wait()
	end

	local connection
	connection = RunService.Heartbeat:Connect(function()
		-- If the character was destroyed and is no longer in Workspace, stop the Heartbeat connection and end the function
		if not character:IsDescendantOf(Workspace) then
			connection:Disconnect()
			return
		end

		if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") and character.Humanoid.Health > 0 then
			arrow.CFrame = CFrame.new(character.HumanoidRootPart.Position + offset, part.Position)
		else
			arrow.Position = Vector3.new(0,-10,0) -- hide the arrow
			return -- This doesn't stop the loop. You can stop the loop by using connection:Disconnect() and storing the connection like above
		end
	end)
end

if player.Character then
	setUpCharacter(player.Character)
end

player.CharacterAdded:Connect(setUpCharacter)

Well basicly the arrow points beside the dumpster.

Some things to check:

  • Make sure the union is anchored
  • Make sure the union’s forward direction is in the direction the arrow shape points (probably the problem)
  • The center of the target part is where you want the arrow to point

If the union is anchored and you think the problem is that the union’s forwards direction is wrong, you can add a little correction rotation using something like this:

arrow.CFrame = CFrame.new(character.HumanoidRootPart.Position + offset, part.Position)
-- Rotate 45 degrees counter-clockwise around the y axis
arrow.CFrame *= CFrame.Angles(math.rad(0), math.rad(45), math.rad(0))

I believe you can also fix the rotation of a union by:

  • Placing a small part inside the union with the correct orientation
  • Select the part first (or last, I forget which one it uses for the orientation)
  • Union the two

I keep getting this error:
17:23:02.422 Players.planeboy2021.PlayerScripts.ObjectiveHandler:28: attempt to index nil with ‘Position’ - Studio

the arrow/union is anchored

local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local camera = Workspace.CurrentCamera
local map = workspace:WaitForChild("OminusOfficesMap"):WaitForChild("Dumpster3")
-- the second parameter in :FindFirstChild(); recursive, allows u to search anywhere within an instance (folder, model, etc) for a specific item. :-)
local part = map:FindFirstChild("Main", true)
local arrow = ReplicatedStorage:WaitForChild("Arrow"):WaitForChild("Arrow"):Clone()
arrow.Parent = Workspace
local offset = Vector3.new(0, 5, 0)

local function setUpCharacter(character)
	while not character:IsDescendantOf(Workspace) do
		task.wait()
	end

	local connection
	connection = RunService.Heartbeat:Connect(function()
		-- If the character was destroyed and is no longer in Workspace, stop the Heartbeat connection and end the function
		if not character:IsDescendantOf(Workspace) then
			connection:Disconnect()
			return
		end

		if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") and character.Humanoid.Health > 0 then
			arrow.CFrame = CFrame.new(character.HumanoidRootPart.Position + offset, part.Position)
			-- Rotate 45 degrees counter-clockwise around the y axis
			arrow.CFrame *= CFrame.Angles(math.rad(0), math.rad(45), math.rad(0))
		else
			arrow.Position = Vector3.new(0, -10, 0) -- hide the arrow
			connection:Disconnect() -- Properly disconnect when character is invalid
		end
	end)
end

if player.Character then
	setUpCharacter(player.Character)
end

player.CharacterAdded:Connect(setUpCharacter)

Make a HumanoidRootPart variable:

local character = player.Character or player.CharacterAdded:Wait
local hrp = character:WaitForChild("HumanoidRootPart")

then change this:

to this:

arrow.CFrame = CFrame.new(hrp.Position + offset, part.Position)

also it might be the part thats not found so for debugging purposes slap a print(hrp) and print(part) right above

Thanks!

The arrow’s pointing direction is a bit off but it’s fine!

Try:

local pos = character.HumanoidRootPart.Position + offset
arrow.CFrame =  CFrame.lookAt(pos , pos + part.Position)

the arrow doesn’t move.

Section of code that is relevant

if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") and character.Humanoid.Health > 0 then
			local pos = character.HumanoidRootPart.Position + offset
			arrow.CFrame =  CFrame.lookAt(pos , pos + part.Position)
		else
			arrow.Position = Vector3.new(0, -10, 0) -- hide the arrow
			connection:Disconnect() -- Properly disconnect when character is invalid
		end

For now just try debugging

if character:FindFirstChild("Humanoid") and character:FindFirstChild("HumanoidRootPart") and character.Humanoid.Health > 0 then
print("1")
			local pos = character.HumanoidRootPart.Position + offset
			arrow.CFrame =  CFrame.lookAt(pos , pos + part.Position)
		else
print("2")
			arrow.Position = Vector3.new(0, -10, 0) -- hide the arrow
			connection:Disconnect() -- Properly disconnect when character is invalid
		end

Ill try to help when Im home in a few hours

Tried that, it printed 1 but the arrow didn’t turn in the direction of the part

And take your time

Try this:

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

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local camera = workspace.CurrentCamera

local arrow = ReplicatedStorage.Arrow.Arrow -- Change this to the correct path of your arrow [DON'T :Clone() IT YET]
local part = workspace.Part -- Change this to the part you want to point to

local offset = Vector3.new(0, 5, 0) -- Edit this how you'd like

local pointing = false
local function PointArrow(partToPointTo: Part)
	pointing = false -- This will erase any arrows made before
	
	if partToPointTo == nil then return end
	local newArrow = arrow:Clone()
	newArrow.Parent = workspace
	
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	
	if humanoid then
		pointing = true
		while pointing and humanoid.Health > 0 do
			local position = hrp.Position + offset
			newArrow.CFrame = CFrame.lookAt(position, position + partToPointTo.Position)

			RunService.Heartbeat:Wait()
		end
	end
	
	newArrow:Destroy() -- Instead of moving it to hide it, just destroy it since a new one is made every time you call this function
end

Then you can call the function whenever with:
PointArrow(part)
or if you have other things in the script that need to run you can call it in another thread:
task.spawn(PointArrow, part)

Also if you want it to stop pointing just set the pointing = false