Rotating and moving a object towards the closest player

Hello, I am making a SCP game, i am making SCP - 999, i have made it get the closest player, but have no clue how to have it rotate towards them and move towards them.

My issue its rotating/moving vectors/Cframes witch i am not good with in scripting.

I have looked at it online and at Cframe/Vector3 related posts, And Cframe/Vector3 in https://developer.roblox.com

My Code so far :

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		while true do
            wait(0.1)
			local vecPoint = script.Parent["Scp-999"].Position
			local closestPlayer, smallestMagnitude = nil,math.huge

			for _, player in pairs(game.Players:GetPlayers()) do
				local distance = player:DistanceFromCharacter(vecPoint)
				if distance < smallestMagnitude then
					smallestMagnitude = distance
					closestPlayer = player

	

				end
			end
		end
	end)
end)
1 Like

You can use this for the rotation:

local gyro = Instance.new("BodyGyro")
gyro.Parent = PrimaryPart
gyro.MaxTorque = Vector3.new(40000,40000,40000)
gyro.CFrame = closestPlayer.HumanoidRootPart.CFrame

And this for the object to move to the player:

for i=0,1,0.01 do -- The last argument determines the speed the part moves at. The bigger it is the faster.
     script.Parent["Scp-999"].CFrame = script.Parent["Scp-999"].CFrame:Lerp(closestPlayer.HumanoidRootPart.CFrame, i)
end

Tell me if these don’t work, and I’ll try to fix it. :smile:
~ ~Grayseon

2 Likes

Ive set up the script like this, is anything wrong im getting an error on this line

error line

gyro.CFrame = closestPlayer.HumanoidRootPart.CFrame

script

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		while true do
			wait(0.1)
			local vecPoint = script.Parent["Scp-999"].Position
			local closestPlayer, smallestMagnitude = nil,math.huge

			for _, player in pairs(game.Players:GetPlayers()) do
				local distance = player:DistanceFromCharacter(vecPoint)
				if distance < smallestMagnitude then
					smallestMagnitude = distance
					closestPlayer = player
					local gyro = Instance.new("BodyGyro")
					gyro.Parent = script.Parent["Scp-999"]
					gyro.MaxTorque = Vector3.new(40000,40000,40000)
					gyro.CFrame = closestPlayer.HumanoidRootPart.CFrame
					
					for i=0,1,0.01 do -- The last argument determines the speed the part moves at. The bigger it is the faster.
						script.Parent["Scp-999"].CFrame = script.Parent["Scp-999"].CFrame:Lerp(closestPlayer.HumanoidRootPart.CFrame, i)
					end
					
				end
			end
		end
	end)
end)
1 Like

What is the error it is outputting?

HumanoidRootPart is not a valid member of Player “Players.bonejon”

Try:

gyro.CFrame = closestPlayer.Character.HumanoidRootPart.CFrame

Instead of:

gyro.CFrame = closestPlayer.HumanoidRootPart.CFrame

It works in a way, but insted of moving SCP - 999 it teleports him to the closest player. And the rotation, he rotates the way that the closest player is looking, not at the closest player.

Heres a clip :