Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

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

Asked by
AmiracIe 175
9 years ago

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!

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. AmiracIe 175 — 9y

1 answer

Log in to vote
3
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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)
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. AmiracIe 175 — 9y
0
I entered a third comma instead of a plus sign, sorry. Let me fix that real quick. adark 5487 — 9y
Ad

Answer this question