Why won't the part spawn infront of the player using LookVector?

  1. What do you want to achieve? I’m trying to make the part spawn infront of the player using LookVector.

  2. What is the issue? It doesn’t spawn infront of the player, rather behind facing down, or infront facing down. This image (The black likes) shows where it spawns because they disappeared when I tried getting a screenshot of them.
    I also get the error Players.YSetWasTaken.Backpack.Pitchfork.ThrowAbilityServer:7: attempt to index nil with ‘CFrame’

This is what I have done

local Event = game.ReplicatedStorage.ThrowAbility
local PitchFork = game.ReplicatedStorage.PitchForkThrown

Event.OnServerEvent:Connect(function()
	local PitchForkClone = PitchFork:Clone()
	PitchForkClone.Parent = game.Workspace
	local lv = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector
	for count = 1,15 do
		wait(1)
		PitchForkClone.CFrame = script.Parent.Parent:FindFirstChild("HumanoidRootPart").CFrame + (lv * 2)
	end
	PitchForkClone:Destroy()
end)

image

Why not just use the parameter OnServerEvent gives you instead of referencing it using script.Parent.Parent? (Just for sanity purposes, make sure to also check if there is a HumanoidRootPart)

local Event = game.ReplicatedStorage.ThrowAbility
local PitchFork = game.ReplicatedStorage.PitchForkThrown

Event.OnServerEvent:Connect(function(Player)
	local PitchForkClone = PitchFork:Clone()
	PitchForkClone.Parent = game.Workspace
	local HRP = Player.Character:FindFirstChild("HumanoidRootPart")
    if HRP then
        local LV = HRP.CFrame.LookVector
	    for count = 1,15 do
		    wait(1)
		    PitchForkClone.CFrame = HRP.CFrame + (LV * 2)
	    end
	    PitchForkClone:Destroy()
    end
end)
1 Like

I added a print to check if it exists

local Event = game.ReplicatedStorage.ThrowAbility
local PitchFork = game.ReplicatedStorage.PitchForkThrown

Event.OnServerEvent:Connect(function(Player)
	local PitchForkClone = PitchFork:Clone()
	PitchForkClone.Parent = game.Workspace
	local HRP = Player.Character:FindFirstChild("HumanoidRootPart")
	if HRP then
		print("Exists")
		local LV = HRP.CFrame.LookVector
		for count = 1,15 do
			wait(1)
			PitchForkClone.CFrame = HRP.CFrame + (LV * 2)
		end
		PitchForkClone:Destroy()
	end
end)

it prints it as exists but still spawns them at the same area

Try printing the CFrame of where that PitchForkClone is exactly going :thinking: Maybe you messed something up with your calculation?

Yeah, I messed up something bc it prints the same CFrame
But I have no clue about what I messed up :sweat_smile:

Are you getting any other errors on your console apart from the one you mentioned in your post?

local Event = game.ReplicatedStorage.ThrowAbility
local PitchFork = game.ReplicatedStorage.PitchForkThrown

Event.OnServerEvent:Connect(function(Player)
	local PitchForkClone = PitchFork:Clone()
	PitchForkClone.Parent = game.Workspace
	local HRP = Player.Character:FindFirstChild("HumanoidRootPart")
	if HRP then
		print("Exists")
		PitchForkClone.CFrame = PitchForkClone.CFrame + (HRP.CFrame.lookVector*5)
		print("Thing")
		wait(3)
		PitchForkClone:Destroy()
	end
end)

After trying a few more things, I’m seeing that it’s ignoring the

PitchForkClone.CFrame = PitchForkClone.CFrame + (HRP.CFrame.lookVector*5)

because it prints both Exists and Thing, and deletes aswell.
I’m not really sure what I’m doing wrong here

Also, no. I don’t get anything else in the console besides that.

Since you’re trying to make the part spawn infront of the player, you should be adding the LookVector to the HumanoidRootPart’s CFrame instead of the pitchfork’s CFrame

local HRPCFrame = HRP.CFrame
PitchForkClone.CFrame = HRPCFrame + (HRPCFrame.LookVector * 5) -- note: "lookVector" with a lowercase "l" is deprecated, you should use "LookVector" instead (uppercase "L")

Edit: Also consider cloning after checking for the existence of the player’s HumanoidRootPart, and also setting the CFrame before parenting the clone

2 Likes

either put this line inside the for loop (right after the wait(1))

local LV = HRP.CFrame.LookVector

or don’t use LookVector at all and change this line

PitchForkClone.CFrame = HRP.CFrame + (LV * 2)

to this

PitchForkClone.CFrame = HRP.CFrame * CFrame.new(0, 0, 2)

That seems to fix it’s spawning, but it for some reason keeps changing it’s orientation to face downwards instead of going forward

That might be because the Pitchfork’s CFrame originally had some rotation in it which is now getting overwritten, you can add it back with CFrame.Angles() like this (you might have to change what axis is being rotated)

local HRPCFrame = HRP.CFrame
PitchForkClone.CFrame = (HRPCFrame + HRPCFrame.LookVector * 5) * CFrame.Angles(math.rad(90), 0, 0) -- note that CFrame.Angles takes radians, not degrees, you have to convert to radians using math.rad
1 Like

When I do this it says
Players.YSetWasTaken.Backpack.Pitchfork.ThrowAbilityServer:10: invalid argument #1 (CFrame expected, got Vector3)

Could you post the new code? _

Yeah, my bad.

local Event = game.ReplicatedStorage.ThrowAbility
local PitchFork = game.ReplicatedStorage.PitchForkThrown

Event.OnServerEvent:Connect(function(Player)
	local PitchForkClone = PitchFork:Clone()
	PitchForkClone.Parent = game.Workspace
	local HRP = Player.Character:FindFirstChild("HumanoidRootPart")
	if HRP then
		local HRPCFrame = HRP.CFrame
		PitchForkClone.CFrame = HRPCFrame + (HRPCFrame.LookVector * 3) * CFrame.Angles(math.rad(90), 0, 0)
		PitchForkClone.Anchored = true
		for count = 1,40 do
			wait()
			PitchForkClone.CFrame = PitchForkClone.CFrame + (PitchForkClone.CFrame.LookVector * .7)
		end
		PitchForkClone.Anchored = false
		PitchForkClone:Destroy()
	end
end)

The order of operations is important when working with CFrames, currently

(HRPCFrame.LookVector * 3) * CFrame.Angles(math.rad(90), 0, 0)

is evaluated first (multiplication comes before addition), resulting in a CFrame, then tries to add that CFrame to HRPCFrame. Adding a CFrame to another CFrame doesn’t work, causing the error. (In my opinion that error message is a bit vague, something like the error message that appears when you try to add a string with a string would be better: “error: attempt to perform arithmetic (add) on CFrame and CFrame”)

Anyways, you can either move the first parenthesis to the front

PitchForkClone.CFrame = (HRPCFrame + HRPCFrame.LookVector * 3) * CFrame.Angles(math.rad(90), 0, 0)

or just wrap the first bit in parenthesis

PitchForkClone.CFrame = (HRPCFrame + (HRPCFrame.LookVector * 3)) * CFrame.Angles(math.rad(90), 0, 0)
1 Like