Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Part creation and positioning?

Hey,

I'm trying to make a part via a script which goes straight above the players head (Well 200 studs above)

Everything works apart from it does not go above the player... It goes up the Y but not to X or Z.

local Player = game.Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local X = Player.Character.Head.Position.X
local Z = Player.Character.Head.Position.Z
local Y = Player.Character.Head.Position.Y

function Click()
local Part = Instance.new("Part")
Part.Anchored = true
Part.Size = Vector3.new(2, 2, 2) 
Part.Position = Vector3.new(X, Y+200, Z) 
Part.CFrame = Part.CFrame * CFrame.fromEulerAnglesXYZ(-90,0,0)
Part.Parent = Camera
Camera.CameraType = "Scriptable"
Camera.CoordinateFrame = Camera.Part.CFrame
Camera.CameraSubject = Camera.Part.MenuCam

end
script.Parent.MouseButton1Down:connect(Click)

2 answers

✓ Accepted answer

Its because you was setting the Position of the head before clicking then the position of the X,Y,Z was the position taken when the script was running.

Here what I do its only Set the position to X,Y,Z when you clicking on the button and if already a part in the camera its will not create another.

The code here

local Player = game.Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera

function Click()
	if not Camera:FindFirstChild("Part") then
		local X = Player.Character.Head.Position.X
		local Z = Player.Character.Head.Position.Z
		local Y = Player.Character.Head.Position.Y
		local Part = Instance.new("Part")
		Part.Anchored = true
		Part.Size = Vector3.new(2, 2, 2) 
		Part.CFrame = CFrame.new(X, Y+200, Z) *CFrame.fromEulerAnglesXYZ(-90,0,0)
		Part.Parent = Camera
		Camera.CameraType = "Scriptable"
		Camera.CoordinateFrame = Camera.Part.CFrame
		Camera.CameraSubject = Camera.Part.MenuCam
	end
end
script.Parent.MouseButton1Down:connect(Click)

You made your variables equal to properties (or actaully the properties of properties, but it's the same idea). This mistake is made a lot. See, when you do this, no reference to the object is kept. The variable will become what the property equals, but it will not change if the property changes later, and editing it will not edit the property. Again, this is because no reference to the original object is kept.

Your X, Y, and Z properties are numbers, and nothing more. It's the same as doing something like

X = 5
Y = 10
Z = 4

Make your variables equal to objects. Then you can access their properties.

local head = Player.Character:WaitForChild("Head")
----
Part.Position = Vector3.new(head.Position.X, head.Position.Y + 200, head.Position.Z)

1 comment

0

your answer's fine

This discussion is preserved as it was. New answers, comments, and votes are closed.