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

How to get Vector3 location on multiple parts?

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

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)

5 comments

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.

0

well you didn't say u used gui .-.

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.

0

I had the ends in there I just forgot to copy them lol

0

Try this i edit again

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

5 comments

0

I edit the script try it

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

0

you want to teleport random to pad or what? i dont understand tbh

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.

0

Ohhh Got it!

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