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

Part creation and positioning?

Asked by
Zyleak 70
9 years ago

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

Log in to vote
4
Answered by 9 years ago

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)

Ad
Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

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)
0
your answer's fine Perci1 4988 — 9y

Answer this question