Part moves too far (TweenService)

Hello!

I was bored and I made this thing (I don’t know how to name it).
Problem is in part position, because it moves too far.
Here is how it looks:

Here is code:

local TweenService = game:GetService("TweenService")

local TInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

local Plates = game.Workspace.Plates:GetChildren()

local random = Random.new()

local function createRay()
	local randomPlate = random:NextInteger(1, #Plates)
	
	local startPos = workspace.Mover.Position
	local endPos = Plates[randomPlate].Position
	
	local result = workspace:Raycast(startPos, endPos - startPos)
	
	if result then
		return result
	end
end

local function destroyPlate()
	local plate = createRay()
	if plate then
		local newTween = TweenService:Create(workspace.Mover, TInfo, {CFrame = CFrame.new(plate.Position.X, workspace.Mover.Position.Y, plate.Position.Z)})
		newTween:Play()
		newTween.Completed:Connect(function()
			task.wait(3)
			plate.Instance:Destroy()
			newTween:Destroy()
		end)
	end
end

while task.wait(5) do
	createRay()
	destroyPlate()
end

The problem with the code is that it is using the Raycast function to find the position of the plate, but it is not using the returned position properly. Instead, it is using the Position property of the returned RaycastResult object to set the CFrame of the Mover object, which is causing it to move too far.

To fix this, you can use the HitPosition property of the RaycastResult object instead of the Position property. The HitPosition property returns the position of the hit object, which is the plate in this case.

Here is the updated code with the fix applied:

local TweenService = game:GetService("TweenService")

local TInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

local Plates = game.Workspace.Plates:GetChildren()

local random = Random.new()

local function createRay()
	local randomPlate = random:NextInteger(1, #Plates)
	
	local startPos = workspace.Mover.Position
	local endPos = Plates[randomPlate].Position
	
	local result = workspace:Raycast(startPos, endPos - startPos)
	
	if result then
		return result
	end
end

local function destroyPlate()
	local plate = createRay()
	if plate then
		-- Use the HitPosition property of the RaycastResult object instead of the Position property
		local newTween = TweenService:Create(workspace.Mover, TInfo, {CFrame = CFrame.new(plate.HitPosition.X, workspace.Mover.Position.Y, plate.HitPosition.Z)})
		newTween:Play()
		newTween.Completed:Connect(function()
			task.wait(3)
			plate.Instance:Destroy()
			newTween:Destroy()
		end)
	end
end

while task.wait(5) do
	createRay()
	destroyPlate()
end

This should fix the issue with the Mover object moving too far.