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)
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
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.