Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to Ignore Y and X Axis in CFrame?

I'm making a script where when the player gets near the NPC, the NPC looks at the player. It works but I want it to ignore the Y and X axis.

script.Parent.Touched:connect(function(Char)

	local Humanoid = Char.Parent:FindFirstChild("Humanoid")

	if Humanoid ~= nil then

	local torso = Char.Parent:FindFirstChild("HumanoidRootPart")

	while wait() do
			script.Parent.Parent.HumanoidRootPart.CFrame = CFrame.new(script.Parent.Parent.HumanoidRootPart.Position, torso.Position)
			end
			end
end)

How do I go about doing this?

1 answer

✓ Accepted answer

I think what you are really asking is for is how to set the "look at" position so that it's the same height as the NPC's HumanoidRootPart. That way, we can "look at" a Position, while only taking into account its X and Z coordinates, and we won't topple over our NPC.

Here is your code with the above change, plus some clean-up

script.Parent.Touched:connect(function(Char)
    local Humanoid = Char.Parent:FindFirstChild("Humanoid")
    if Humanoid ~= nil then
        local torso = Char.Parent:FindFirstChild("HumanoidRootPart")
        local rotatingPart = script.Parent.Parent.HumanoidRootPart
        while wait() do
            local lookAt = Vector3.new(
                torso.Position.X,
                rotatingPart.Position.Y,
                torso.Position.Z
            )
            rotatingPart.CFrame = CFrame.new(rotatingPart.Position, lookAt)
        end
    end
end)

2 comments

0

It works in Studio but in-game, it just disappears

0

Nvm fixed it tysm

This discussion is preserved as it was. New answers, comments, and votes are closed.