What do you want to achieve? I’m trying to make the part spawn infront of the player using LookVector.
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)
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)
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
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
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
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
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