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

Why can't I create a part three studs away from the player's torso by using lookVector?

Essentially, I want a part to be created three studs away from the player's torso when another part is touched. I want it to be three studs away from the torso at all times and also be ahead of it - no other direction. I hope to achieve this by using lookVector, but another method will also be accepted if it gets the job done. Just in case you need some more imagery, it should resemble a fairy following you.

Here's my code:

local pad=script.Parent
local DistanceAwayFromPlayer=3 



pad.Touched:connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		print(hit.Parent.Torso.CFrame.lookVector)
		part=Instance.new("Part",workspace)
		part.Anchored=true
		part.Size=Vector3.new(1,1,1)
		fire=Instance.new("Fire")  
		fire.Size=Vector3.new(1,1,1)
		--part.Transparency=1
		fire.Parent=part
		pad.Parent=game.ServerStorage
			while wait() do
				part.CFrame=hit.Parent.Torso.CFrame.lookVector +  Vector3.new(0,0,DistanceAwayFromPlayer)
			end	
	end
end)

This is the error I'm receiving:

22:11:18.492 - Workspace.Pad.Script:18: bad argument #3 to 'CFrame' (CFrame expected, got userdata)

The script creates the part and everything but puts it at the center of the map rather than having it three studs ahead of the torso. Any help on this matter would be greatly appreciated!

1 comment

0

Please do not deterred to answering this question by the large amount of words. I used a lot of words so that you guys could better understand my problem.

1 answer

✓ Accepted answer

First of all, this isn't an optimal way to achieve this effect. A Weld would do you much better.

The lookVector of a CFrame is a normalized vector in the direction of the Front normal of that CFrame. That is, it has a magnitude of exactly 1. You have to add it to the Torso's Position to get this code to work.

Since it already has a magnitude of 1 stud, you multiply it to get the distance of three studs that you want:

part.CFrame = CFrame.new(hit.Parent.Torso.CFrame.lookVector*3 +  hit.Parent.Torso.Position)

This will make it almost-constantly be 3 studs in front of the player, but it will always be facing the positive X axis (iirc). To fix that, we need to give a second parameter, something for the CFrame to 'look' at. In this case, one stud further away:

local lookVector = hit.Parent.Torso.CFrame.lookVector
part.CFrame = CFrame.new(lookVector*3 +  hit.Parent.Torso.Position, lookVector*4 + hit.Parent.Torso.Position)

2 comments

0

The first code works, but when you added the second parameter, it stopped working. I will gladly accept your answer if you can solve this. The output shows no errors.

0

I entered a third comma instead of a plus sign, sorry. Let me fix that real quick.

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