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

How would I create a part where a player is currently standing?

Asked by 7 years ago
Edited 7 years ago

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)

0
Parts (or parts in general) have a two properties: Position and CFrame (Coordinate Frame). You can set either of the properties to the player's character (but for CFrame, you need to retrieve a part of the player's character, like the Torso); however, if you want the part to be on top of the part, Position is the way to go. TheeDeathCaster 2368 — 7y
0
To add onto my last comment: Think of CFrame as a definite position, while Position is where you don't have to set up calculations to have it be above other parts. TheeDeathCaster 2368 — 7y
0
Okay Let me try something and Ill let ya know if i fixed it. GuestRealization 102 — 7y
0
Okay I'm using position and it is still not working I'll update the question. GuestRealization 102 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

  1. CFrame

  2. Position

  3. Character and Humanoid

  4. The CharacterAdded Function

  5. The WaitForChild Function

  6. The Spawn Coroutine

IT WORKED!!!! :O

Hope this helped you in any way! :D

Ad
Log in to vote
0
Answered by 7 years ago

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)
0
Okay says CFrame is not valid member of part, I get that but what should CFrame be then? GuestRealization 102 — 7y
0
Okay okay for some reason it didn't work but now it does o_0 GuestRealization 102 — 7y

Answer this question