How to make a Object Spawn in the Direction the Player is Looking

I have these 2 parts that tween into teach other but they always spawn in one direction. How could I change this so that the part spawns in front of the direction the player is looking?

game.ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local tweenservice = game:GetService('TweenService')
	local clone = game.ReplicatedStorage.IceLeftWall:Clone()
	local char = plr.Character
	clone.CFrame = char.LowerTorso.CFrame*CFrame.new(0,0,5)
	clone.Position = Vector3.new(char.HumanoidRootPart.Position.X -80, char.HumanoidRootPart.Position.Y , char.HumanoidRootPart.Position.Z - 60)
	clone.Rotation = Vector3.new(0, 270, 0)
	clone.Parent = char
	local clone2 = game.ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.LowerTorso.CFrame*CFrame.new(0,0,5)
	clone2.Position = Vector3.new(char.HumanoidRootPart.Position.X +80, char.HumanoidRootPart.Position.Y , char.HumanoidRootPart.Position.Z - 60 )
	clone2.Rotation = Vector3.new(0, 90, 0)
	clone2.Parent = char
	
	local tweeninfo = TweenInfo.new(
		1,	
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	local tween = tweenservice:Create(clone, tweeninfo, {Position = Vector3.new(clone.Position.X + 80, clone.Position.Y, clone.Position.Z)})
	local tween2 = tweenservice:Create(clone2, tweeninfo, {Position =  Vector3.new(clone2.Position.X - 80, clone2.Position.Y, clone2.Position.Z)})
	tween:Play()
	tween2:Play()
	tween.Completed:Wait()
	clone:Destroy()
	clone2:Destroy()
end)

You can use the LookVector to achieve this.

New code:

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

--//Controls
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end
	
	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 5
	clone.Parent = char
	
	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 5
	clone2.Parent = char
	
	local tween = TweenService:Create(clone, tweeninfo, {Position = Vector3.new(clone.Position.X + 80, clone.Position.Y, clone.Position.Z)})
	tween:Play()

	local tween2 = TweenService:Create(clone2, tweeninfo, {Position = Vector3.new(clone2.Position.X - 80, clone2.Position.Y, clone2.Position.Z)})
	tween2:Play()
	tween2.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)

Sorry for the late response, but this happens, it tweens in the other direction and is sideways. I will attach a video
robloxapp-20220918-1511109.wmv (450.7 KB)

Try this:

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

--//Controls
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end

	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = (char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 20) * CFrame.fromEulerAngles(0, math.rad(90), 0)
	clone.Parent = char

	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = (char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 20) * CFrame.fromEulerAngles(0, math.rad(90), 0)
	clone2.Parent = char

	local tween = TweenService:Create(clone, tweeninfo, {Position = Vector3.new(clone.Position.X + 80, clone.Position.Y, clone.Position.Z)})
	tween:Play()

	local tween2 = TweenService:Create(clone2, tweeninfo, {Position = Vector3.new(clone2.Position.X - 80, clone2.Position.Y, clone2.Position.Z)})
	tween2:Play()
	tween2.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)

Its also bugged for all directions
robloxapp-20220918-1522520.wmv

I know the problem it is because in the code it says:

clone2.Position.X - 80

Meaning it’ll only tween out of the X axis!

I’ll attempt to rewrite it and see if this works!

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

--//Controls
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end
	
	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 5
	clone.Parent = char
	
	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 5
	clone2.Parent = char
	
	local tween = TweenService:Create(clone2, tweeninfo, {CFrame = CFrame.new(char.HumanoidRootPart.CFrame.X + 80, char.HumanoidRootPart.CFrame.Y, char.HumanoidRootPart.CFrame.Z)})
	tween:Play()

	local tween2 = TweenService:Create(clone2, tweeninfo, {CFrame = CFrame.new(char.HumanoidRootPart.CFrame.X - 80, char.HumanoidRootPart.CFrame.Y, char.HumanoidRootPart.CFrame.Z)})
	tween2:Play()
	tween2.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)

I hope this helps I am not sure if it works. Let me know!

The same issue is there

If you didnt know this is what I want (I want this to spawn in the direction the player is looking)
robloxapp-20220918-2138284.wmv (591.3 KB)

And this is whats happening
robloxapp-20220918-2136507.wmv (1.7 MB)

So you want them in the direction the player is facing and shoot out that way too?

I want them to crash into each other in the direction their facing (first video from the post above), again sorry for the late response.

Try this:

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

--//Controls
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end

	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 20 + -char.CFrame.RightVector * 40
	clone.Parent = char

	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.LowerTorso.CFrame + char.LowerTorso.CFrame.LookVector * 20 + char.CFrame.RightVector * 40
	clone2.Parent = char

	local leftWallTween = TweenService:Create(clone, tweeninfo, {CFrame = clone.CFrame + clone.CFrame.LookVector * 30})
	leftWallTween:Play()

	local rightWallTween = TweenService:Create(clone2, tweeninfo, {CFrame = clone.CFrame + clone.CFrame.LookVector * 30})
	rightWallTween:Play()
	rightWallTween.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)

I get an error
image

I changed char.CFrame into char.PrimaryPart.CFrame and its like this now, also the error is gone

robloxapp-20220919-1604236.wmv (1.9 MB)

I want it to be like this (link below) but for the direction the player is looking at

It also goes a little bit into the ground, I just want it to stay on ground level

Okay, I tried rotating the walls to the right way, try this:

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

--//Controls
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end

	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = char.PrimaryPart.CFrame + char.PrimaryPart.CFrame.LookVector * 20 + -char.PrimaryPart.CFrame.RightVector * 40
	clone.CFrame *= CFrame.fromEulerAngles(0, math.rad(-90), 0)
	clone.Parent = char

	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.PrimaryPart.CFrame + char.PrimaryPart.CFrame.LookVector * 20 + char.PrimaryPart.CFrame.RightVector * 40
	clone2.CFrame *= CFrame.fromEulerAngles(0, math.rad(90), 0)
	clone2.Parent = char

	local leftWallTween = TweenService:Create(clone, tweeninfo, {CFrame = clone.CFrame + clone.CFrame.LookVector * 30})
	leftWallTween:Play()

	local rightWallTween = TweenService:Create(clone2, tweeninfo, {CFrame = clone.CFrame + clone.CFrame.LookVector * 30})
	rightWallTween:Play()
	rightWallTween.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)

Wow, your really close, but one of them is making a 180 degree turn, I will attach a video on whats happening
robloxapp-20220919-1622079.wmv (810.1 KB)

Okay this should work perfectly then:

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

--//Controls
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end

	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = char.PrimaryPart.CFrame + char.PrimaryPart.CFrame.LookVector * 20 + -char.PrimaryPart.CFrame.RightVector * 40
	clone.CFrame *= CFrame.fromEulerAngles(0, math.rad(-90), 0)
	clone.Parent = char

	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.PrimaryPart.CFrame + char.PrimaryPart.CFrame.LookVector * 20 + char.PrimaryPart.CFrame.RightVector * 40
	clone2.CFrame *= CFrame.fromEulerAngles(0, math.rad(90), 0)
	clone2.Parent = char

	local leftWallTween = TweenService:Create(clone, tweeninfo, {Position = clone.Position + clone.CFrame.LookVector * 30})
	leftWallTween:Play()

	local rightWallTween = TweenService:Create(clone2, tweeninfo, {Position = clone.Position + clone.CFrame.LookVector * 30})
	rightWallTween:Play()
	rightWallTween.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)
1 Like

And your correct! But can you make it so that it spawns a bit more forward? It spawns inside of the player and kills them

Okay, here, you can edit the variable depending on how far forward you want.

New code:

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

--//Controls
local forwardDistance = 50
local tweeninfo = TweenInfo.new(
	1,	
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

--//Functions
ReplicatedStorage.Remotes.IceWall.OnServerEvent:Connect(function(plr)
	local char = plr.Character

	if not char then
		return
	end

	local clone = ReplicatedStorage.IceLeftWall:Clone()
	clone.CFrame = char.PrimaryPart.CFrame + char.PrimaryPart.CFrame.LookVector * 20 + -char.PrimaryPart.CFrame.RightVector * 40
	clone.CFrame *= CFrame.fromEulerAngles(0, math.rad(-90), 0)
	clone.Parent = char

	local clone2 = ReplicatedStorage.IceRightWall:Clone()
	clone2.CFrame = char.PrimaryPart.CFrame + char.PrimaryPart.CFrame.LookVector * 20 + char.PrimaryPart.CFrame.RightVector * 40
	clone2.CFrame *= CFrame.fromEulerAngles(0, math.rad(90), 0)
	clone2.Parent = char

	local leftWallTween = TweenService:Create(clone, tweeninfo, {Position = clone.Position + clone.CFrame.LookVector * forwardDistance})
	leftWallTween:Play()

	local rightWallTween = TweenService:Create(clone2, tweeninfo, {Position = clone.Position + clone.CFrame.LookVector * forwardDistance})
	rightWallTween:Play()
	rightWallTween.Completed:Wait()

	clone:Destroy()
	clone2:Destroy()
end)

Oh wait, I edited the script wrong, I’ll fix it real quick.

Yeah, I changed the number to 100 and it glitched out