Help with undesired CFrame movement

Well, here is my code:

script:WaitForChild("Enabled").Changed:Connect(function(Enable)
	print(Enable)
	if Enable == true then
		local UserInputService = game:GetService("UserInputService") 
		local RunService = game:GetService("RunService") 

		local plr= game.Players.LocalPlayer
		local char = game.Workspace:WaitForChild(plr.Name)

		local Cam = game.Workspace.CurrentCamera 
		Cam.CameraType = Enum.CameraType.Scriptable
		Cam.CFrame = game.Workspace.BuildMode.CameraPart.CFrame

		local W,A,S,D,NotMoving = false,false,false,false,true

		local function KeepMoving()
			if EnableVal.Value == true then
				repeat 
					if 	W then 
						Cam.CFrame = Cam.CFrame + (Cam.CFrame.LookVector * 0.2)
					end
					if 	S then 
						Cam.CFrame = Cam.CFrame + (Cam.CFrame.LookVector * -0.2)
					end
					if 	A then 
						Cam.CFrame = Cam.CFrame + (Cam.CFrame.RightVector * -0.2)
					end
					if 	D then 
						Cam.CFrame = Cam.CFrame + (Cam.CFrame.RightVector * 0.2)
					end

					RunService.RenderStepped:Wait()
				until NotMoving or Enable == false
			end
		end


		local function MoveBegan(actionName,inputState,input)
			if EnableVal.Value == true then
				if  input.UserInputType == Enum.UserInputType.Keyboard then

					if UserInputService:IsKeyDown(Enum.KeyCode.W) then
						W = true
						NotMoving= false
					end
					if UserInputService:IsKeyDown(Enum.KeyCode.A) then
						A = true
						NotMoving= false
					end
					if UserInputService:IsKeyDown(Enum.KeyCode.S) then
						S = true
						NotMoving= false
					end
					if UserInputService:IsKeyDown(Enum.KeyCode.D) then
						D = true
						NotMoving= false
					end
				end
			end


			if W and not S and not D and not A then
				if EnableVal.Value == true then
					KeepMoving()
				end
			elseif not W and not S and not D and  A then
				if EnableVal.Value == true then
					KeepMoving()
				end

			elseif not W and not S and not D and  A then
				if EnableVal.Value == true then
					KeepMoving()
				end

			elseif not W and not S and not D and  A then
				if EnableVal.Value == true then
					KeepMoving()
				end

			end

		end 


		local  function MoveEnded(input, gpe)
			if EnableVal.Value == true then
				if input.KeyCode == Enum.KeyCode.W then
					W = false
				elseif input.KeyCode == Enum.KeyCode.S then
					S = false
				elseif input.KeyCode == Enum.KeyCode.D then
					D = false
				elseif input.KeyCode == Enum.KeyCode.A then
					A = false
				end
			end

		end

		if EnableVal.Value == true then
			if not W and not S and not D and not A then	
				NotMoving = true
			else
				NotMoving = false
			end
		end

		local dx, dy = 0, 0 -- in degrees
		local MouseSensitivity = 0.2

		local UserInputService = game:GetService("UserInputService")
		local Mouse = game.Players.LocalPlayer:GetMouse()
		local Cam = workspace.CurrentCamera

		local function UpdateModel()
			if EnableVal.Value == true then
				
				Cam.CFrame = Cam.CFrame:lerp(CFrame.new(Cam.CFrame.Position)
					* CFrame.fromOrientation(0, math.rad(-dy), 0),0.2) -- Change lerp if u want
			end
		end

		--math.rad(dx)

		local function MouseMove()
			if EnableVal.Value == true then
				local Delta = UserInputService:GetMouseDelta()
				dx += Delta.Y * MouseSensitivity
				dy += Delta.X * MouseSensitivity
				UpdateModel()
			end
		end

		local function MouseDown()
			if EnableVal.Value == true then
				UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			end

		end

		local function MouseUp()
			if EnableVal.Value == true then
				UserInputService.MouseBehavior = Enum.MouseBehavior.Default
			end

		end
		
		
		
		local defaultSpeed = script:GetAttribute('Speed')
		local defaultCameraY = script:GetAttribute('Height')
		local defaultCameraOrientation = script:GetAttribute('Orientation')
		local defaultZoomSpeed = script:GetAttribute('ZoomSpeed')
		local heightRange = script:GetAttribute('HeightRange')
		local dynamicSpeed = script:GetAttribute('DynamicSpeed')

		local speed = defaultSpeed
		local cameraY = math.clamp(defaultCameraY,heightRange.Min,heightRange.Max)
		local cameraOrientation = CFrame.Angles(math.rad(defaultCameraOrientation),0,0)
		local zoomSpeed = defaultZoomSpeed

		local T = game:GetService('TweenService')
		local function tween(o,t,l,s,d)
			if EnableVal.Value == true then
				s = s or Enum.EasingStyle.Linear
				d = d or Enum.EasingDirection.InOut
				local i = TweenInfo.new(l,s,d)
				return T:Create(o,i,t)
			end
		end

		local RS = game:GetService('RunService')
		local Players = game:GetService('Players')
		local player = Players.LocalPlayer
		local mouse = player:GetMouse()
		local cam = workspace.CurrentCamera

		local function updateCamera()
			if EnableVal.Value == true then
				local pos = cam.CFrame.Position
				cameraOrientation = CFrame.Angles(math.rad(cameraY/defaultCameraY*defaultCameraOrientation),0,0)
				tween(cam,{CFrame=CFrame.new(pos.X,cameraY,pos.Z)*cameraOrientation},0.1):Play()
			end
		end

		mouse.WheelForward:Connect(function()
			if EnableVal.Value == true then
				cameraY = math.clamp(cameraY-zoomSpeed,heightRange.Min,heightRange.Max)
				updateCamera()
			end
		end)

		mouse.WheelBackward:Connect(function()
			if EnableVal.Value == true then
				cameraY = math.clamp(cameraY+zoomSpeed,heightRange.Min,heightRange.Max)
				updateCamera()
			end
		end)
		
		

		Mouse.Move:Connect(MouseMove)
		Mouse.Button2Down:Connect(MouseDown)
		Mouse.Button2Up:Connect(MouseUp)

		if EnableVal.Value == true then
			game:GetService("ContextActionService"):BindAction("MoveCam" , MoveBegan, false,Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D)
			
			UserInputService.InputEnded:Connect(MoveEnded)
		end
	else
		local plr= game.Players.LocalPlayer
		local UserInputService = game:GetService("UserInputService") 
		local Cam = game.Workspace.CurrentCamera 
		Cam.CameraType = Enum.CameraType.Custom
		Enable = false
		UserInputService.MouseBehavior = Enum.MouseBehavior.Default
		local function MoveBegan(actionName,inputState,input)
			print("Played this function")
		end
		game:GetService("ContextActionService"):UnbindAction("MoveCam" , MoveBegan, false,Enum.KeyCode.W, Enum.KeyCode.A, Enum.KeyCode.S, Enum.KeyCode.D)
	end
end)

Idk if you want to take the time and go through it (it mostly looks long cause of all the varibles and functions. It shouldn’t be too hard to navigate to where I change the cframes, ect.). What it’s supposed to do is when the value is true, make the player’s camera to go to a cam position in the workspace and then allow the player to move around using w,a,s,d and pan left or right using mousebutton2 and scroll up or down. But… this is the result:

Code so-far demonstration.mp4 — Google drive link (vid was too big to upload to dev forum)

I think the scrolling part on the script and my the part that allows the player to pan the camera are having trouble with each other.
Happy Easter!
It would be fantastic if somebody could help.
Kind Regards,
Creative.

1 Like

please put your video on the developer forum instead its a lot more convenient when your pc is really bad at rendering videos

1 Like

It wouldn’t let me upload the video to the dev forum so I used google drive instead as it doesn’t seam to have a limit on video size, anyway. Can you help me with my issue?

1 Like