How to move a part towards another at a constant pace

would this work?

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

local player = Players.LocalPlayer
local part = workspace.VoicesPart -- set this to the location of the part
local speed = 5 -- Adjust the speed

RunService.RenderStepped:Connect(function(deltaTime)
    if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
        local playerPosition = player.Character.HumanoidRootPart.Position
        local partPosition = part.Position
        local direction = (playerPosition - partPosition).Unit
        local newPosition = partPosition + direction * speed * deltaTime
        part.CFrame = CFrame.new(newPosition, playerPosition)
    end
end)

1 Like

This works!

Heres my code if anyone is wondering:

while true do
	task.wait(0.1)
	
	script.Parent.CFrame = CFrame.lookAt(script.Parent.Position,hrp.Position)
	script.Parent.Position = script.Parent.Position + script.Parent.CFrame.LookVector * 2
	
	for i,v in pairs(workspace:GetPartBoundsInBox(script.Parent.CFrame,script.Parent.Size)) do --ignore this part (its for hit detection)
		if v == hrp then
			hrp.Anchored = true
			game.Players.LocalPlayer:Kick("VEhFWSBIQVZFIEZPVU5EIFlPVS4=")
		end
	end 
end
2 Likes

Glad to see that works!

Also, a better performant way may be to check the Magnitude rather than repeatedly checking what parts are in bounds of the player.

Like this:

(part.CFrame.Position-target.CFrame.Position).Magnitude < 0.5
2 Likes