Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why will this not make a new part at the characters head?

Asked by 3 years ago

here's the LOCAL script:

--in a local script, instantiate all unchanging variables outside of functions
local localPlayer = game.Players.LocalPlayer
local PlayerName = localPlayer.Name
local WorkspaceCharacter = game.Workspace:WaitForChild(PlayerName)
local charHead = WorkspaceCharacter:WaitForChild("Head")
local charPos = charHead.Position
-- why set the left foot of the character or the player name to a variable if you never use it?

script.Parent.MouseButton1Click:Connect(function()
    local NewPart = Instance.new("Part")--second arg is deprecated
    NewPart.Position = Vector3.new(charPos)--don't forget to close your functions
    NewPart.Parent = game.Workspace --set the parent after setting all of the other properties of the new part
end)

1 answer

Log in to vote
0
Answered by
Sparks 534 Moderation Voter
3 years ago

Changing the Position will not clip the part into the character's head. You will need to change the part's CFrame for this. You should also make sure that the part is anchored. Furthermore, you are only getting the player's head position once, when the script is first ran. To get the head position every time the mouse button is clicked, you need to define that variable inside the function every time it is ran.

For the part to go inside the head:

--in a local script, instantiate all unchanging variables outside of functions
local localPlayer = game.Players.LocalPlayer
local PlayerName = localPlayer.Name
local WorkspaceCharacter = game.Workspace:WaitForChild(PlayerName)
local charHead = WorkspaceCharacter:WaitForChild("Head")
-- why set the left foot of the character or the player name to a variable if you never use it?

script.Parent.MouseButton1Click:Connect(function()
    local charPos = charHead.CFrame
    local NewPart = Instance.new("Part")
    NewPart.Anchored = true
    NewPart.CFrame = charPos

    --I am turning CanCollide off so the player doesn't get flung or something similar
    NewPart.CanCollide = false 
    NewPart.Parent = game.Workspace
end)

For the part to go on top of the head:

--in a local script, instantiate all unchanging variables outside of functions
local localPlayer = game.Players.LocalPlayer
local PlayerName = localPlayer.Name
local WorkspaceCharacter = game.Workspace:WaitForChild(PlayerName)
local charHead = WorkspaceCharacter:WaitForChild("Head")
-- why set the left foot of the character or the player name to a variable if you never use it?

script.Parent.MouseButton1Click:Connect(function()
    local charPos = charHead.CFrame * CFrame.new(0, charHead.Size.Y/2, 0)
    local NewPart = Instance.new("Part")
    NewPart.Anchored = true
    NewPart.CFrame = charPos

    --I am turning CanCollide off so the player doesn't get flung or something similar
    NewPart.CanCollide = false 
    NewPart.Parent = game.Workspace
end)
0
i was trying to figure out how to make it so it spawns in front of the character so how would i teleport the block and then add onto the cframe to make it anchored and collide on but not going into the character diggerbear1 32 — 3y
0
You can add the lookVector of the body part you want it to go in front of. Sparks 534 — 3y
Ad

Answer this question