Part spawning in front of you

Does anyone know how to make this script make it so the cube will spawn in front of you?

RemoteEvent Name: CUBE

Script in ServerScriptService:

local Event = game.ReplicatedStorage.CUBE

Event.OnServerEvent:Connect(function(Player)

local Part = Instance.new("Part")

Part.Size = Vector3.new(2, 2, 2)

Part.Parent = game.Workspace

Part.BrickColor = BrickColor.new("Ghost grey")

Part.Material = Enum.Material.SmoothPlastic

end)

LocalScript in StarterPlayerScripts:

local UIS = game:GetService("UserInputService")
local Event = game.ReplicatedStorage.CUBE

UIS.InputBegan:Connect(function(key,typing)
	if typing then return end
	if key.KeyCode == Enum.KeyCode.K then
		Event:FireServer()
	end
end)

I’m only looking to make it so it spawns in front of you

Set the CFrame to

Part.CFrame = player.Character.HumanoidRootPart.CFrame + Vector3.new(0,0,3) -- or whatever position you want

Where would I put that at and how do I set a CFrame?

An easier way would be is to just reference it’s LookVector

local Event = game.ReplicatedStorage.CUBE

Event.OnServerEvent:Connect(function(Player)
    local Part = Instance.new("Part")
    Part.Size = Vector3.new(2, 2, 2)
    Part.Parent = game.Workspace
    Part.BrickColor = BrickColor.new("Ghost grey")
    Part.Material = Enum.Material.SmoothPlastic
    Part.CFrame = CFrame.new(Player.Character.HumanoidRootPart.Position + (Player.Character.HumanoidRootPart.CFrame.LookVector * 10))
end)

This would set its position to be where your Character is currently looking at, and you can set its 10 Number to how far you want it to spawn in front of you

3 Likes

Thank you so so much! Very much appreciated!