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

make sure part has a certain name, then get its properties?

I'm trying to make a gui stay where ever a part is in the workspace, and to prevent me from having to write hundreds of repeated lines of code, I want to make a system in the gui that will detect if your near a button. I'm most of the way done with this code:

for _,i in pairs(game.Workspace:GetDescendants)
if i:IsA("UnionOperation") and i.Name == "Button" then
--i dont know what to put here, this would be what tells the script it is a button
end 
--[[local V2 = WorldToScreen(what do i put here so that i can get the parts in the workspace that have the name button and access their position?? )]]--

so basically i dont know how to make sure a part has a certain name and the get its position

4 comments

0

I Didnt Quite Understand Your Question, what Do You Exactly Want To Do?

0

After Accessing The Buttons Position?

0

I would want to create a variable of some sort that contains the position of the part if it has the name "Button"

0

Use CollectionService instead of game.Workspace:GetDescendants().

3 answers

✓ Accepted answer
for _,i in pairs(game.Workspace:GetDescendants()) do

if i:IsA("UnionOperation") and i.Name == "Button" then
--i dont know what to put here, this would be what tells the script it is a button
variablePos = i.Position -- this gives you the position. 
end 
end

To determine the position you could put all of this in a local script and define the player with

But you'd need a function to update this because the for loop will run instantly. You could use a loop like while true do or repeat until

local player = game:GetService("Players").LocalPlayer
if player:DistanceFromCharacter(pos) < DISTyouWantToBeWithin then -- change the distance.
--<code>
end

Hmm maybe.. You Can Return The Buttons Position By Writing :

return (i.Position)

this basically returns the buttons/Current index's Position And Prints It In The OutPut I Think.

3 comments

0

i thought about using return, but then how would i use that later on in the code when i need the position?

0

Just use local Pos = i.Position

0

Sorry If It Was Wrong Your Question Was Kinda Confusing And What You Were Trying To Do Wasn't Clear.

I would use a dictionary for this. I'm not sure exactly what you're trying to do, but I think I got the gist.

First, set up the dictionary

local positions = {}

Then, do what you did while adding the part(s) to the table and add a changed event (you'd have to figure out if its a button yourself because I'm not sure exactly what you mean)

for _,v in pairs(game.Workspace:GetDescendants()) do
	if v:IsA("UnionOperation") and v.Name == "Button" then
		--Check if it's a button
		positions[v] = v.Position
		v:GetPropertyChangedSignal("Position"):Connect(function()
			positions[v] = v.Position
		end)
	end
end 

Now, whenever you want to get the positions, all you have to do is

for i,v in pairs(positions) do
	print(i.Name.." is currently at") -- Button is currently at..
	print(v) -- Position
	-- idk u can figure out what u want to do with this
end

Hope it helps!

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