How to make part spawn in direction player is looking at

I have a script that clones 3 rings and places it at the player. What happens is the rings always spawn at a certain angle, and not toward the player. How can I make this so the clones always spawn in front of the player?

Script:

game.ReplicatedStorage.Remotes.Uppercut.OnServerEvent:Connect(function(plr)
	local character = plr.Character
	local humanoidrootpart = character.HumanoidRootPart
	
	local shockwave = game.ReplicatedStorage.Moves.EpicPunch.FirstRing:Clone()
	shockwave.Parent = workspace.Fx
	shockwave.Position = Vector3.new(humanoidrootpart.Position.X, humanoidrootpart.Position.Y, humanoidrootpart.Position.Z + 3)
	local shockwave1 = game.ReplicatedStorage.Moves.EpicPunch.SecondRing:Clone()
	shockwave1.Parent = workspace.Fx
	shockwave1.Position = Vector3.new(humanoidrootpart.Position.X, humanoidrootpart.Position.Y, humanoidrootpart.Position.Z + 5)
	local shockwave2 = game.ReplicatedStorage.Moves.EpicPunch.ThirdRing:Clone()
	shockwave2.Parent = workspace.Fx
	shockwave2.Position = Vector3.new(humanoidrootpart.Position.X, humanoidrootpart.Position.Y, humanoidrootpart.Position.Z + 7)
end)

Video: https://streamable.com/6bikhc

I would want it to be like this
image

Instance.CFrame.LookVector

You can also just use CFrame * newCFrame

Use LookVector.

Fixed code:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Functions
ReplicatedStorage.Remotes.Uppercut.OnServerEvent:Connect(function(player)
	local character = player.Character
	local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
	
	if not humanoidRootPart then
		return
	end

	local shockwave = ReplicatedStorage.Moves.EpicPunch.FirstRing:Clone()
	shockwave:PivotTo(humanoidRootPart.CFrame + humanoidRootPart.CFrame.LookVector * 3)
	shockwave.Parent = workspace.Fx
	
	local shockwave1 = ReplicatedStorage.Moves.EpicPunch.SecondRing:Clone()
	shockwave1:PivotTo(humanoidRootPart.CFrame + humanoidRootPart.CFrame.LookVector * 5)
	shockwave1.Parent = workspace.Fx
	
	local shockwave2 = ReplicatedStorage.Moves.EpicPunch.ThirdRing:Clone()
	shockwave2:PivotTo(humanoidRootPart.CFrame + humanoidRootPart.CFrame.LookVector * 7)
	shockwave2.Parent = workspace.Fx
end)
2 Likes

You cant add a CFrame with a Vector3, instead what you have to do is this:

shockwave.CFrame = humanoidRootPart.CFrame * CFrame.new(0,0,5)

Which would easily simplify your code

I tried your code, but what happens is they are all flat instead of this.
image

Video: https://streamable.com/o6awq7

I don’t understand LookVector that well. I’ve tried using it, and what happens is it ends up flat and somewhere random.

LookVector is the front position of an Object.

Object Orientation is probably messed up

1 Like

You could use CFrames in this case to get it facing in front of you whenever triggering the remote.

game.ReplicatedStorage.Remotes.Uppercut.OnServerEvent:Connect(function(plr)
	
	local character = plr.Character
	local HMR = character.HumanoidRootPart

	local shockwave = game.ReplicatedStorage.Moves.EpicPunch.FirstRing:Clone()
	shockwave.Parent = workspace.Fx
	shockwave.CFrame = HMR.CFrame * CFrame.new(0,0,-3) * CFrame.Angles(math.rad(-90),0,0)
	
	local shockwave1 = game.ReplicatedStorage.Moves.EpicPunch.FirstRing:Clone()
	shockwave1.Parent = workspace.Fx
	shockwave1.CFrame = HMR.CFrame * CFrame.new(0,0,-5) * CFrame.Angles(math.rad(-90),0,0)
	
	local shockwave2 = game.ReplicatedStorage.Moves.EpicPunch.FirstRing:Clone()
	shockwave2.Parent = workspace.Fx
	shockwave2.CFrame = HMR.CFrame * CFrame.new(0,0,-7) * CFrame.Angles(math.rad(-90),0,0)
end)

Assuming it’s flat at CFrame.Angles(0,0,0), setting it to CFrame.Angles(math.rad(-90),0,0) would make the front surface face forwards. If not, feel free to experiment with the values until you get your desired orientation.

Alright, I’ll let you know if this works.

Yes! It works like a charm! Thanks for the help @DasKairo @Katrist @kiddomaru

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.