I’m trying to make a script that would change the transparency of all the parts inside of a group, and it seems to be working for the most part, but wouldn’t change the transparencies of all the parts.
This is the script
local debounce = false
function Click()
local children = game.Workspace.MatMarkers1:GetChildren()
if debounce == false then
for i = 1, #children do
local child = children[i]
child.Transparency = 0.5
print("hi")
debounce = true
end
elseif debounce == true then
for i = 1, #children do
local child = children[i]
child.Transparency = 1
print("Hai")
debounce = false
end
end
end
script.Parent.ClickDetector.MouseClick:Connect(Click)
I would try using a for in pairs do loop which is just a for loop designed to loop through dictionaries, arrays, etc. (This is what GetChildren() returns). Using this will help lower clutter in the script.
If any errors were being shown in the output of your previous script it would help since the issue may not be with the code. But I would try the code I shown below.
local debounce = false
local function Click()
for _,child in ipairs(game.Workspace.MatMarkers1:GetChildren()) do
if debounce then
child.Transparency = 0.5
else
child.Transparency = 1
end
debounce = not debounce
end
end
script.Parent.ClickDetector.MouseClick:Connect(Click)
I’m not sure why it’s not changing the transparency of all the Children but you should add an if statement
if Child:IsA("BasePart") then end
so your code is less prone to errors, and you can also use ChildAdded to change the transparency of newly added Children (or catch ones that are late)
also use ipairs for arrays. GetChidren returns an Array
Seems like you have a weird Debounce boolean assignment going on, can you explain what you are trying to achieve so we can provide better code for your use case scenario?
Basically, I’m trying to make a part that when clicked, it would change the transparencies of all the parts within a specific group/folder. Clicking it once would turn it on, clicking it again would turn it off.
I’m on my iPad so you’ll have to type it out yourself but I’ll guide you.
local transparency = isTransparent and 1 or .5 — flip transparency
for _, child in ipairs(YourModel:GetChildren()) do
if child:IsA("BasePart") then
child.Transparency = transparency
end
end
isTransparent = (not isTransparent) — flip isTransparent
You can also use this with YourModel.ChildAdded
Whoops! the code runs only once or might not at all, sorry about that let me fix it!
Hey, I wrote it out as this, is this the correct way to write it out?
function Click()
local children = game.Workspace.MatMarkers1:GetChildren()
if isTransparent then
local transparency = isTransparent and .5
for _, child in ipairs(MatMarkers1:GetChildren()) do
if child:IsA("BasePart") then
child.Transparency = transparency
end
end
isTransparent = (isTransparent)
end
if isTransparent then
local transparency = isTransparent and 1
for _, child in ipairs(MatMarkers1:GetChildren()) do
if child:IsA("BasePart") then
child.Transparency = transparency
end
end
isTransparent = (not isTransparent)
end
end
script.Parent.ClickDetector.MouseClick:Connect(Click)
You need to declare “isTransparent” as a local variable and you can replace my code with your click function, you should name it with something more meaningful like “toggleTransparency”
Objects created on the server replicate to all the clients. Objects created on a client do not replicate to the server. you might wanna check where the objects are created. because if the getchildren() is serversided or on a different player’s client and the objects are created on your client, there will be issues.
you will be able to circumvent this by using remotevents to send instructions from the client to the server so it get’s created there.
Or you could put the GetChildren() in the a localscript so it runs clientsided.
I ended up fixing this by restarting studio, and using my original script. Not sure what happened, I’m assuming it must have been an issue with my computer.