Making a part follow my character while staying above their head

This is the code I’ve tried doing but I keep getting the same output.
Output: Workspace.Script:5: attempt to index nil with ‘Character’

local player = game.Players.LocalPlayer

local function followPlayer()
	while true do
		local character = player.Character
		if character == nil then
			wait()
			continue
		end

		local humanoid = character:WaitForChild("Humanoid")
		local headPosition = humanoid.HumanoidRootPart.Position
		local part = workspace.MyPart
		part.Position = headPosition + Vector3.new(0, 1, 0)
		wait(0.1)
	end
end

local part = Instance.new("Part")
part.Name = "MyPart"
part.Size = Vector3.new(1, 1, 1)
part.Position = Vector3.new(0, 0, 0)
part.Parent = workspace

followPlayer()

try use this:

local Player = game:GetService("Players").LocalPlayer

local Part = Instance.new("Part", workspace)
Part.Name = "MyPart"
Part.Size = Vector3.one
Part.Anchored = true

while task.wait(.1) do
	local Character = Player.Character
	if Character then
		Part.Position = Character.Head.Position+Vector3.yAxis
	end
end
  1. Use RunService.Heartbeat to run functions each frame
  2. Use If-Expressions to evaluate the existence of objects. Try to stay away from :WaitForChild since it’s easy to misuse, and in this case unnecessary since you’re code is going to be ran each frame anyways.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local LocalPlayer = Players.LocalPlayer
local Part = ...

local OFFSET: Vector3 = Vector3.yAxis

local function UpdatePart(): ()
	local Character: Model? = LocalPlayer.Character
	local Humanoid: Humanoid? = if Character then Character:FindFirstChildWhichIsA("Humanoid") else nil
	local RootPart: BasePart? = if Humanoid then Humanoid.RootPart else nil
	
	if RootPart then
		Part.Position = RootPart.Position + OFFSET
	end
end

RunService.Heartbeat:Connect(UpdatePart)

To make a part follow your character’s head in a Roblox game, you can use the HumanoidRootPart property of the character to get the position of the character’s head, and then use the CFrame property of the part to position it above the head. Here is an example of how you can do this:

local character = game.Players.LocalPlayer.Character
local head = character:WaitForChild("Head") -- get the character's head
local part = script.Parent -- the part that will follow the head

while true do
  -- use the head's position to set the part's CFrame
  part.CFrame = CFrame.new(head.Position + Vector3.new(0, 1, 0)) -- position the part 1 unit above the head
  wait(0.1) -- wait 0.1 seconds before updating the part's position again
end

This code uses a while loop to continuously update the position of the part based on the position of the head. The CFrame of the part is set to a new position that is 1 unit above the head’s position. The loop waits 0.1 seconds before updating the position again, to avoid updating the position too frequently.

You can adjust the position of the part relative to the head by modifying the Vector3 value in the CFrame.new function. For example, you could use Vector3.new(0, 2, 0) to position the part 2 units above the head, or Vector3.new(0, 1, 1) to position the part 1 unit above and 1 unit in front of the head.

I hope this helps! Let me know if you have any questions.