I have a script here that I didn't make and modified it in attempt to make it look towards the nearest player. When I test the script, it fails and says that 'Character' is a nil value.

local player = game.Players:GetChildren()
local character = player.Character.Torso
local part = script.Parent

local function faceNearestBlock(part, blocks, maxDistance)
    local closestPart
    local minDistance = maxDistance
    for _, partToFace in ipairs(character) do
        local distance = (part.Position - partToFace.Position).magnitude
        if distance < minDistance then
            closestPart = partToFace
            minDistance = distance
        end 
    end
    if closestPart then
        part.CFrame = CFrame.new(part.Position, closestPart.Position)
    end
end

faceNearestBlock(part, parts, 100)

The original script worked though, and you can find it here.

What am I doing wrong?

1 comment

0

You literally posted a perfect answer for your code and you're asking us to answer it again?