Part Keeps Randomly Flying Off The Map And Then Moving To The BasePlate

  1. I Need: A Moving part that goes back and forth, specificly for a character to hit a door and go backwards a few times.

  2. Issue: The Part just moves instantly and then flies below the map. without dissapearing

  3. Ive Tried:
    Checking Cframe related Parts of the script
    checking for typos

doing any of these just makes the whole part stay in place.

The Script:

local RunService = game:GetService("RunService")
local Val = game.ServerScriptService
local Aval = Val.CurrentRoom
local npc = script.Parent
npc.RushNew.CFrame = workspace.Things.Back.CFrame
npc.RushNew.Spawn:Play()
wait(1)


local function Move(MainPart, Update)	
	local TheEvent = Instance.new("BindableEvent")
	local GetUpdated = Update or false

	local function GetPlacePosition()
		return MainPart.Position
	end

	local Moving
	Moving = RunService.Heartbeat:Connect(function(deltaTime) 
		local Speed = 400
		local PlaceToMove = GetPlacePosition()
		local MainPosition = npc.RushNew.Position
		local Direction = (PlaceToMove - MainPosition).Unit
		local Movin = Speed * deltaTime
		local NewMain = MainPosition * Direction * Movin
		npc.RushNew.CFrame = CFrame.new(NewMain)

		local DistNew = (NewMain - PlaceToMove).Magnitude
		if DistNew < 6 then
			Moving:Disconnect()
			TheEvent:Fire()
		end
	end)

	local NewSignal
	if GetUpdated then
		NewSignal = Aval.Changed:Connect(function()
			MainPart = game.Workspace.Things.Front
		end)
	end
	TheEvent.Event:Wait()
	if NewSignal then
		NewSignal:Disconnect()
	end
end

local Yo = workspace.Things

for i = 0, math.random(1, 10) do
Move(Yo.Front, true)
Move(Yo.Back, false)
end
npc.RushNew:Destroy()

I cannot seem to figure out why the part flies off the map instead of going to the door and back to the first waypoint like its supposed to.

This is the easiest way I’ve known to make something like this. I don’t know what error I’ve Made.
Thanks if anyone can help.

Couldnt you just use a hinge to move the door? or am I just misunderstanding something?

it is a script meant to move a character (aka a part) to the door of the current room that’s been generated. by making the part move back and forth between the starting position and the door itself.
but instead of doing that, the script just sends the part below the baseplate in my game. that’s the issue.