So i’m having issues trying to get a part to spawn infront of the player.
Right now, It spawns at the HumanoidRootPart’s position and copy it’s rotation but i want it to spawn 1 stud away from the HumanoidRootPart.
This is my code right now.
-- define the tool
local tool = script.Parent
-- define the function to create a brick
local function createBrick(position, rotation)
local brick = Instance.new("Part")
brick.Size = Vector3.new(1, 1, 1)
brick.Position = position
brick.Orientation = rotation
brick.Anchored = true
brick.CanCollide = false
brick.Parent = workspace
print("Created")
end
-- connect the tool's activation event to create a brick at the player's position
tool.Activated:Connect(function()
local player = game.Players.LocalPlayer
local character = player.Character
local rootPart = character.HumanoidRootPart
if rootPart then
createBrick(rootPart.Position, rootPart.Orientation)
end
print("Activated")
end)
Instead of setting Position and Orientation, you can use CFrame (coordinate frame). To spawn a part in front of the HumanoidRootPart, we can multiply the root part’s CFrame by a new CFrame moving it forward on Z-axis relative to the root’s orientation.
Then in the createBrick(cf), we would modify the CFrame property of a part:
local function createBrick(cframe)
local brick = Instance.new("Part")
brick.Size = Vector3.new(1, 1, 1)
brick.CFrame = cframe
brick.Anchored = true
brick.CanCollide = false
brick.Parent = workspace
print("Created")
end
I would also suggest declaring those variables in .Activated once on top of the script, as such:
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local rootPart = character:WaitForChild("HumanoidRootPart")
It’s very important to wait for the parts to load to prevent any errors raised because of at the time missing parts.
EDIT: Please keep in mind that the parts created locally on the client’s side aren’t replicated to the server and consequentially to other players. If you want a global part, you will have to send a remote event and ask the server to create one for you.