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

How to get Vector3 location on multiple parts?

Asked by 4 years ago

Lets say there are multiple objects in a workspace with the same name: item & item

They are both located under game.Workspace.Terrain.item

I want to be able to grab the position of them and set that to a variable as follows in terms of the Vector3 location

local test = game.Workspace.Terrain.item.Position

I've been able to do this with objects that are in the game as long as there is only one of them with a name that isn't being shared by another object. What I want to do currently works if there is only one item.

How do I grab and save the position of multiple items with the same name and workspace location?

My ultimate goal is to be able to split up the two items with their different vectors so I can save the coordinates as local x = item.x etc. Is there a way to maybe split the returned vectors into an array and do something like local x = item[1].x?

2 answers

Log in to vote
0
Answered by
G2001H 75
4 years ago
Edited 4 years ago
local parts = workspace.Terrain:GetChildren()
local char = workspace:FindFirstChild(game.Players.LocalPlayer.Name)
script.Parent.MouseButton1Click:Connect(function()
    char.HumanoidRootPart.CFrame = parts[math.random(#parts)].CFrame
end)
0
I have a GUI and I am attempting to teleport my character to the location of a part on the map that shares the same name of other parts. This code does work for a single part but still not for multiple. I tested it with local test = v.Position and then attempting to teleport to the location but there is an error thrown when trying to get the x,y,z of the variable test. ben453245 0 — 4y
0
well you didn't say u used gui .-. G2001H 75 — 4y
0
I just posted the code I tried on the thread here. Again said code does work as written if there is only a single object under terrain by that name. ben453245 0 — 4y
0
I had the ends in there I just forgot to copy them lol ben453245 0 — 4y
0
Try this i edit again G2001H 75 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
for i,v in pairs(game.Workspace.Terrain:GetChildren()) do
    if v:IsA("Part") and v.Name == "Pad" then --classname and name of part
    local test = v.Position
    local x = test.x
    local y = test.y
    local z = test.z
    game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name).HumanoidRootPart.CFrame = CFrame.new(Vector3.new(x,y+5,z)) 
    print("ran")
    break

Error in trying to get the position of v

0
I edit the script try it G2001H 75 — 4y
0
I tried it with a single object on the map with the name brick. It successfully teleports me to that object and breaks with no error. There are multiple objects with the shared name of pad but the code won't run till the end, it gets stuck on local test = v.Position ben453245 0 — 4y
0
you want to teleport random to pad or what? i dont understand tbh G2001H 75 — 4y
0
well there are multiple pads and I want to be able to teleport to the first one that the code finds. I can add in stuff later to refine it more but I just need the general concept of what I'm trying to do to work. ben453245 0 — 4y
0
Ohhh Got it! G2001H 75 — 4y

Answer this question