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

How Do I Get A Brick To Face A Player Using Cframes?

I've looked up everything I could think of and tried a lot and i can get this to work.

local Part_To_Face = game.Workspace.Part
local Target_To_Face = game.Players:GetChildren()

while true do
	Part_To_Face.CFrame = CFrame.new(Part_To_Face.Position, Target_To_Face.Position)
	wait(0.01)
end

Here's the Error Code:

13:41:43.525 - Workspace.Part.Script:5: invalid argument #2 to 'new' (Vector3 expected, got nil)
13:41:43.525 - Stack Begin
13:41:43.526 - Script 'Workspace.Part.Script', Line 5
13:41:43.526 - Stack End

1 answer

✓ Accepted answer

you cant do Target_To_Face.Position because you are getting the children but not their characters, on top of that you will need to loop


while true do
local Part_To_Face = game.Workspace.Part
local Target_To_Face = game.Players:GetChildren()
	local closest = Vector3.new(0,0,0)
	for _, v in pairs(Target_To_Face) do
		if v.Character then
			if closest == nil then closest = v.Character.HumanoidRootPart.Position Part_To_Face.CFrame = CFrame.new(Part_To_Face.Position, closest) end

			if (v.Character.HumanoidRootPart.Position - Part_To_Face.Position).Magnitude < (closest-Part_To_Face.Position).Magnitude then closest = v.Character.HumanoidRootPart.Position Part_To_Face.CFrame = CFrame.new(Part_To_Face.Position, closest) end
		end
	end
    wait(0.01)
end

Basically what this does is, it loops through all the players and finds their humanoidrootpart, the closest one is what the part will face at.

Tell me if it works, click accept if it does!

11 comments

0

Sadly It does not work.

0

What type of script are you using?

0

Also try now, at first I thought you were posting a snippet of the script but then I realized it was the whole script, I made an edit

0

It still doesnt work. 14:26:04.931 - Workspace.Part.Script:12: attempt to index nil with 'Position' 14:26:04.932 - Stack Begin 14:26:04.932 - Script 'Workspace.Part.Script', Line 12 14:26:04.932 - Stack End

0

Should I be using a localscript?

0

Try now, I removed the .Position and just put in the variable when it was set, if it doesnt work then i will put it in the if statement

0

Still doesn't work

0

Now this is the error: 15:01:37.307 - Workspace.Part.Script:4: attempt to index nil with 'Character'

0

Sorry im kinda being useless here, im new to lua scripting.

0

IT WORKS THANKS SO MUCH!

0

np xd

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