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

Getting the direction of a part help?

Alright, So apparently I'm supposed to get the direction of a part then find part on ray, but how am I getting the direction of a part with position?

'Players.Player.Backpack.LocalScript:21: attempt to index field '?' (a nil value)'

local walls = game.Workspace.Walls:GetChildren()
local chartorso = game.Players.LocalPlayer.Character.Torso
local lowname = nil
local lowest = 20

for i = 1, #walls do
	walls[i].Name = "Wall"..i
end

function onKeyPress(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.E then
		for i,v in pairs (walls) do
			if math.ceil((chartorso.Position-v.Position).magnitude) < lowest then
				lowest = math.ceil((chartorso.Position-v.Position).magnitude)
				lowname = v.Name
			end
		end
		print(lowname)
		print(chartorso.Position)
		
		local ray = Ray.new(chartorso.Position, walls[lowname].Position * 300)
		
		local hit, position = game.Workspace:FindPartOnRay(ray)
	
		for i = 1, #walls do
			walls[i].BrickColor = BrickColor.new("Smoky grey")
		end
		game.Workspace.Walls[lowname].BrickColor = BrickColor.Green()
		lowest = 20
	end
end
 
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 answer

You can't with position however you can with CFrame. Don't ask me how though, I am not quite that advanced in math. The standard Vector3 value that you use with CFrame.new() is a tip of the ice berg. CFrame can also give current rotation as well as it's position. I would look it up on the wiki as it explains the rotation matrix a bit more.

2 comments

0

I think I misunderstood you question. You are looking for how to get the direction of a part compared to the position of another object aren't you.

0

ye

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