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!
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)