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

Need help selecting bricks in a model using a script?

Hi. So with this script, I'm trying to make it so when you click a GUI button, it makes the parts in the certain model either visible or not. (on and off system). The script is correct, but I just need help making it so it selects the bricks in a model and changes their properties accordingly (instead of listing something like World.Brick1.Transparency = 1 and changing that for 20 different bricks) Any help is appreciated!

local P = script.Parent.Parent.Parent.Parent.Parent.Name
local HLoc = Workspace:FindFirstChild(P)
local On = false

function onClick(mouse)
	local Helmet = HLoc:findFirstChild("Chest")
	local belt = Workspace.Equipment.Chest.refvest:GetChildren()
	if Helmet ~= nil and On==false then
		
		belt.Transparency = 0
		
	On = true
	elseif Helmet ~= nil and On==true then
		
		belt.Transparency = 1
		
	On = false
	end
end

script.Parent.MouseButton1Down:connect(onClick)

2 answers

✓ Accepted answer

Use a for loop:

for _, v in pairs (object:GetChildren()) do
	if v:IsA("Part") then
		v.Transparency = 1
	end
end

Hope this helped.

EDIT: This was an example, but to toggle transparency you can do it like this: (The 'object' variable was just an example.)

local On = false
function toggle(model)
	for _, v in pairs (model:GetChildren()) do
			if v:IsA("Part") then
				v.Transparency = On and 1 or 0 -- If On is true, set it to 1. Otherwise, set it to 0.
			end
		end
		On = not On -- Toggle the On variable.
	end
function onClick()
	local Helmet = HLoc:findFirstChild("Chest")
	local belt = Workspace.Equipment.Chest.refvest
	if Helmet then
		toggle(belt)
	end
end

8 comments

0

Running into issues with that. I don't know if this would change your answer, but this is the layout of the model and other things: http://prntscr.com/bkhxfo This is the script I edited with your code: http://prntscr.com/bkhy32

0

Is this what the script should look like? http://prntscr.com/bkio9p

0

No. In the 2nd example, you would use the word 'model' in the toggle function as I had it, not refvest. Apologies for switching from an example to an actual function.

0

Is this correct? (http://prntscr.com/bkk13t) If so, its not working..

0

You never connected onClick to MouseButton1Down..

0

It works, however, its supposed to make a copy of the models and put it on your person, then when you click the button it changes the transparency of the stuff on you. http://prntscr.com/bkk5rm

0

Alright, well that wasn't a part of your question at all. I'd recommend consulting the wiki for stuff like :Clone() and welds. If this solved the problem, you should accept the answer so people know it's been answered.

0

After attempting to create the next part of your script, don't be afraid to ask another question if you can't quite get it.

Well if you don't mind, I have a much more simple method to get what you're trying to do.

You can try to do this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

mouse.Button1Down:connect(function()
	if mouse.Target.Transparency == 1 and --[[Is in the model]] then 
	mouse.Target.Transparency = 0 
	elseif mouse.Target.Transparency == 0 and --[[Is in the model]] then 
	mouse.Target.Transparency = 1 
	print(mouse.Target.Name) --Just to help show functionality

end
end)

I am assuming you know how to use if statements based on your code above so this should be easy for you to edit the if statement to get what you need.

Just put this code in a local script and put it in either StarterGui or StarterPack.

Hope this helps.

1 comment

0

This is helpful and I appreciate it, just not what I was exactly looking for. This won't work for the system I'm doing. If this helps, here you go! this is the layout of the model and other things: http://prntscr.com/bkhxfo

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