So, I’m making a script and trying to make it to where all parts with the name “GreenPart” have edits to them, but it’s not working, instead only 1 of them does it.
Code
function onTouched(part)
wait()
game.Workspace.GreenPart.Transparency = 0
game.Workspace.GreenPart.CanCollide = true
As you referenced a specific one it will only edit a single one.
Instead if you do :GetChildren() and iterate through the table, adding the effects to ones where the name == “GreenPart” you’ll be able to effect them all.
-- add the .Touched event here
for i, v in pairs(workspace:GetChildren()) do -- for all the children of workspace this for loop will run.
if v.Name == "GreenPart" then -- if a child in workspace has the name "GreenPart" it will be destroyed.
v:Destroy()
end
end
Roblox allows you to name several instances with the same name in the same hierarchy, when indexing game.Workspace with ‘GreenPart’ you aren’t asking it to find all game objects with the name ‘GreenPart’, you’re just indexing one game object in Workspace with the name ‘GreenPart’.
As @SpacialEthanRB stated, you need to find all the children of workspace with the name ‘GreenPart’ and then change the properties on each instance.
This is what your code is doing:
function onTouched(part) -- Define a function called 'onTouched' with the arguments 'Part'
wait() --> wait 1 frame
game.Workspace.GreenPart.Transparency = 0 --> Change a single game object's transparency called 'GreenPart' within Workspace to 0
game.Workspace.GreenPart.CanCollide = true --> Change a single game object's collision property called 'GreenPart' within Workspace to 'true'
end
script.Parent.Touched:connect(onTouched) --> When the parent of this script is touched, run the function 'onTouched' (also sends the argument of what touched it, which in your case will be defined as 'part')
This is what it should be doing:
function onTouched(part)
wait() -- You probably don't actually need this unless you want to wait 1 frame before doing the rest?
--> Collect all the children of 'game.Workspace' and iterate through them, providing the index (the key) and the object (the value) of the table the :GetChildren() method returns
for index, object in next, game.Workspace:GetChildren() do
--> Check if that object is a 'Part', if not, we can ignore it
if object:IsA('Part') then
--> Check if the object's name is 'GreenPart', if not, we can ignore it once again
if object.Name == 'GreenPart' then
--> Okay great, we've found another object called 'GreenPart' in workspace, let's change the transparency and collision now!
object.Transparency = 0
object.CanCollide = true
end
end
end
end
script.Parent.Touched:connect(onTouched)
Some caveats: you should probably add a debounce to the code above, as you only need to run this code once (unless the transparency and collision resets). Otherwise, you’ll be looping through the entire children of workspace every time a part comes into contact with the script’s parent.
I found a bug in your script. There should be a line of code which is testing who touched the part. That can be a player or another part. I tested that in studio. Use this code below:
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
for i, v in pairs(game.Workspace:GetChildren()) do
if v.Name == "GreenPart" then
v.Transparency = 0
v.CanCollide = true
end
end
end
end)
Not necessarily a bug as I just went off what OP had initially posted, he may be wanting a instance e.g. a car to hit the script’s parent. However, this fits in well with the debounce suggestion, and is a good learning point for the OP in the future!
As @The_Woozoo stated, you should ensure that you’re confirming the object that touched the script’s parent is the object that you’d like to initiate this event and as mentioned and should ensure that you add in confirmatory statements to determine whether you need to perform this action again or if it’s completed its functionality and would be detrimental to performance / superfluous. Please do note this @gumixchan, and please do read the comments in the code.