How would I make a block follow infront of a player?

How would I make a block that follows Infront of a player, instead of simply placing Infront of it?
What I have so far:

local wrksp = game:GetService("Workspace")
local builditems = repst.BuildItems
local b1 = builditems.trainblock
local player = game.Players.LocalPlayer
local clickframe = script.Parent
local Character = player.Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local cplace = script.Parent.ConfirmPlace
local Time =  tick()

clickframe.MouseButton1Click:Connect(function()
	local b1 = builditems.trainblock:Clone()
	b1.Transparency = 0.5
	b1.Material = "ForceField"
	b1.BrickColor = BrickColor.new(3,165,0)
	b1.Parent = wrksp
	b1.CFrame = CFrame.new(HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y-2.25, HumanoidRootPart.Position.Z-2.25)
	cplace.Visible = true 
	if tick() - Time < 0.5 then -- tick() - Time would be the time elapsed
		print("double Click!")
		cplace.Visible = false
	end
	Time = tick()
	
end)```

You’ll want to make a loop that repositions the part frequently.

while true do
	task.wait()
	-- Code to position part here
end

How would I do that?
Can I update the position of the part by figuring out when the player moves and adjusting it forward or something? I’m not sure.

local wrksp = game:GetService("Workspace")

local builditems = repst.BuildItems
local b1 = builditems.trainblock
local player = game.Players.LocalPlayer
local clickframe = script.Parent
local Character = player.Character
local humanoid: Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local cplace = script.Parent.ConfirmPlace
local Time =  tick()

local connection: RBXScriptConnection

local function StartFollowing(part: BasePart)
	if connection and connection.Connected then
		connection:Disconnect()
	end
	
	connection = humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
		part.CFrame = HumanoidRootPart.CFrame - Vector3.new(0, 2.25, 2.25)
	end)
end

local function StopFollowing(part: BasePart)
	if connection and connection.Connected then
		connection:Disconnect()
	end
	
	part:Destroy()
end

clickframe.MouseButton1Click:Connect(function()
	local b1 = builditems.trainblock:Clone()
	b1.Transparency = 0.5
	b1.Material = "ForceField"
	b1.BrickColor = BrickColor.new(3,165,0)
	b1.Parent = wrksp
	
	StartFollowing(b1)
	
	cplace.Visible = true 
	if tick() - Time < 0.5 then -- tick() - Time would be the time elapsed
		print("double Click!")
		cplace.Visible = false
	end
	Time = tick()
end)
while true do
    task.wait()
    b1.CFrame = HumanoidRootPart.CFrame * CFrame.new(0, 0, -3) --3 studs in front of player
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.