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)
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.
~ ~Grayseon
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)
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.