This is what I have so far, it is a basic script that just creates a part and anchors it but how would i make it create the part whereever the player is standing? What would I use?
local player = game.Players.LocalPlayer local h = player.Character.Humanoid local button = script.Parent local main = script.Parent.Parent button.MouseButton1Click:connect(function() local part = Instance.new("Part", workspace) part.Anchored = true end)
UPDATE CODE (STILL NOT WORKING)
local player = game.Players.LocalPlayer local h = player.Character.Humanoid local button = script.Parent local main = script.Parent.Parent local plrh = player.Character.Head local plrhp = player.Character.Head.Position button.MouseButton1Click:connect(function() local part = Instance.new("Part", workspace) part.Position = plrhp end)
This answer is to be only used as a reference, and no other purpose.
Just like I explained in my comments, you can set a part's - or parts' - CFrame property to that of another part; it's the same with Position, but Position makes it so that part(s) go above a part so that you don't have to do extra work with CFrame to accomplish that.
Here's an example:
local lastPart = nil -- Not necessary, it's just for example of how CFrame and Position can be used; what this does is it defines the part after a new one has been created, then when a new part is create, it will be set above the last part that was created. (Confusing! T-T) function createNewPart() local newPart = Instance.new('Part') -- Creates & defined a new part. (Durr) if lastPart then -- If there was a part created previously by the script, execute the following: newPart.Position = lastPart.Position -- Sets the newly created part's Position to that of the last part else -- Otherwise newPart.CFrame = game.Workspace.BasePlate.CFrame + Vector3.new(0, 1, 0) -- Sets the first new part to the default BasePlate (upon created a new BasePlate plain game) end newPart.Parent = game.Workspace -- Sets the new part's parent to the Workspace lastPart = newPart -- The new part is now defined as the lastPart end for i = 1, 2 do -- Fires 2 times; why repeat the same function two times? (Retyping it over-and-over I mean.) createNewPart() -- Fires the function; when you call upon a function's name, it'll fire it. end
And when you fire the example code, you will notice that two parts have been created, and one's on top of one another.
Now, from your second code, there's a noticeable but easy to fix problem. The problem I'm speaking of is how you retrieve the player's Character, Humanoid and Torso (no link given because the wiki doesn't have a docu on it ;/ ). When you retrieve them directly as is, your code is subjected to the great possibility of breaking because one of the three may not be there when the code executes. (What I mean is one of them may be nil, not existent yet.)
To counter this, you can use the following:
~ CharacterAdded - This will fire when the player's character loads. (A function of a player only.)
~ WaitForChild - Will yield the code until a child of the same name as the argument given to the function exists within the selected parent that the function is called upon. (OH MY LORDS THAT'S A LOT OF JUNK- I MEAN INFO!)
An example of how to use these is the following:
---------------------------- -- CHARACTERADDED EXAMPLE -- ---------------------------- local player = game.Players.LocalPlayer -- Lets say that we're using a LocalPlayer local character = player.Character or player.CharacterAdded:Wait() -- As you can see, it will try and retrieve the player's character, but if it returns nil, then it'll call upon the CharacterAdded event to wait until the player's character is there, then return it. print(character.Name) -- Whoever the player's name is. >-> -------------------------- -- WAITFORCHILD EXAMPLE -- -------------------------- local newPart = Instance.new('Part') newPart.Name = 'Part' -- Created a new part; yippee... spawn(function() -- spawn is a type of coroutine; only difference is that, from my experience, it doesn't have all the advantages that the other coroutines have. -- The wait begins... print('I\'m waiting!') game.Workspace:WaitForChild('Part') -- As you can see, the WaitForChild function was called upon the Workspace and is now waiting for an object called "Part" to enter the Workspace, and now... print('Torso is here!') -- Player's torso found end) wait(2) -- Waits 2 seconds bewPart.Parent = game.Workspace -- Sets the part's parent to the Workspace
Notice how it took a bit for the prints to fire? That's because when the WaitForChild function is called upon with an argument, it'll yield the code until the specific object enters the parent, and then let the rest of the code pass.
Aaaaaand that's all there's to it actually. lol
Stuff touched on, but didn't go into great detail about
~Attempt to index 'information' (a nil value)~
Ok... Lets try this instead:
Stuff mentioned and touched on
The CharacterAdded Function
The WaitForChild Function
The Spawn Coroutine
IT WORKED!!!! :O
Hope this helped you in any way! :D
Use :GetPrimaryPartCFrame
. This is a function of all models that returns the CFrame
of the PrimaryPart
of that model. The PrimaryPart
of any character is the HumanoidRootPart
of that model.
local player = game.Players.LocalPlayer local h = player.Character.Humanoid local button = script.Parent local main = script.Parent.Parent button.MouseButton1Click:Connect(function() --'connect' is deprecated, use 'Connect' instead local part = Instance.new("Part") --NEVER use the second parameter of Instance.new; it can ruin performance. Instead, define the parent as the last thing you do when you create something. part.Anchored = true local humPos = h.Parent:GetPrimaryPartCFrame() --gets the CFrame of the PrimaryPart part.CFrame = humPos --sets the CFrame of the part as the HumanoidRootPart part.Parent = game.Workspace --set the parent last end)