The parts in the model won't go transparent

I want to change the property of all the parts in a model
but for some reason every code works but the transparency and the CanCollide
I’ve tried changing the contents of the for in pairs loop but it still won’t work I also tried putting if statements in my function to see if it went transparent but it didn’t.

Here is the code for the parts i’m trying to change
I put it in the ServerScriptService

local block = game.Workspace:FindFirstChild("Triggered2")
local myBrick = game.Workspace.Triggered2:GetChildren("EventTriggered2")
local CanRun = true
myBrick.CanCollide = true
myBrick.Anchored = true
myBrick.Transparency = 0
local function ImTriggered(part)
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	if humanoid and CanRun then
		CanRun = false
		print("Did it work?")
		
		myBrick.Transparency = .25 
		wait(.4)
		myBrick.Transparency = 0.5
		end
		wait(.4)
		myBrick.Transparency = 0.75
		wait(.4)
		myBrick.Transparency = 1
		myBrick.CanCollide = false
		if myBrick.CanCollide == false then
			print("Changed")
		end
		print("It worked!")
		wait(3)
		CanRun = true
		myBrick.CanCollide = true
		myBrick.Transparency = 0
		end
	end

for i,n in pairs(block:GetDescendants())do
	if n:isA(block) then
		n.Touched:connect(ImTriggered)
	end
end

Any suggestions will do.

1 Like

You need to call the function properly, so ImTriggered(Part) to call it. As it is even if it fires it wont have the ‘Part’ variable passed on through the function.

On top of that do you have any error codes in your output? (Rember to post any errors you get when posting as it is extremely useful)

1 Like

Shouldn’t it be FindFirstChild?

3 Likes

That line of code is not the problem. Connecting .Touched to any function will always pass over the otherPart as a parameter.

The problem, like @SOTR654 pointed out, is

local myBrick = game.Workspace.Triggered2:GetChildren("EventTriggered2")

Should be

local myBrick = game.Workspace.Triggered2:FindFirstChild("EventTriggered2")

GetChildren() returns a table containing all the children of a specific object. By saying myBrick.Transparency = 0.25, you’re essentially doing myBrick[“Transparency”] = 0.25.

4 Likes

I tried that and it returned this error code

1 Like

It works now but only for the first part in the model. Probably because it’s not reaching the other parts?

What do you mean first part of the model?

Its only working for the first EventTriggered2 part in the Triggered2 model
Screen Shot 2021-03-04 at 1.27.44 PM

You are referencing myBrick in the ImTriggered function, not the part that is actually being touched. Get rid of myBrick all together and replace it in the function with “part”. This way every time the function runs it changes the properties of the correct part, not the first part.

Edit: apologies for not being able to write the correct code, I am currently in school, on my phone.

Edit2: I’m gonna try anyways.

local block = game.Workspace:FindFirstChild(“Triggered2”)
local CanRun = true
myBrick.CanCollide = true
myBrick.Anchored = true
myBrick.Transparency = 0
local function ImTriggered(part)
local humanoid = part.Parent:FindFirstChild(“Humanoid”)
if humanoid and CanRun then
CanRun = false
print(“Did it work?”)

	part.Transparency = .25 
	wait(.4)
	part.Transparency = 0.5
	end
	wait(.4)
	part.Transparency = 0.75
	wait(.4)
	part.Transparency = 1
	part.CanCollide = false
	if part.CanCollide == false then
		print("Changed")
	end
	print("It worked!")
	wait(3)
	CanRun = true
	part.CanCollide = true
	part.Transparency = 0
	end
end

for i,n in pairs(block:GetDescendants())do
if n:isA(block) then
n.Touched:connect(ImTriggered)
end
end

excuse the stupid formatting. Everything I said was the code, not just the code in the code block.

I tried that and it only made my body part disappear since “part” in my code is what’s touching the brick

Ohh I see, so you want the parts within Triggered2 to disappear.

Add another parameter when calling ImTriggered like this:

local block = game.Workspace:FindFirstChild(“Triggered2”)
local children = block:GetChildren()
local CanRun = true
myBrick.CanCollide = true
myBrick.Anchored = true
myBrick.Transparency = 0
local function ImTriggered(part, brick)
local humanoid = part.Parent:FindFirstChild(“Humanoid”)
if humanoid and CanRun then
CanRun = false
print(“Did it work?”)
for i, myBrick in pairs(children) do
myBrick.Transparency = .25
wait(.4)
myBrick.Transparency = 0.5
end
wait(.4)
myBrick.Transparency = 0.75
wait(.4)
myBrick.Transparency = 1
myBrick.CanCollide = false
if myBrick.CanCollide == false then
print(“Changed”)
end
print(“It worked!”)
end
wait(3)
for i, myBrick in pairs(children) do
CanRun = true
myBrick.CanCollide = true
myBrick.Transparency = 0
end
end
end

for i,n in pairs(block:GetDescendants())do
if n:isA(block) then
n.Touched:connect(ImTriggered(n))
end
end

EDIT: fixed the code so all bricks change.

So the code worked but it is starting with the second part, then the third, then the first, then skips to the fourth and fifth. Do you know why that is. I tweaked the n.Touched:connect(ImTriggered(n)) and removed the n because it was returning nil.

That is because whichever part was loaded first in is first into the table, which due to a new update is now random. Does it need to happen in a specific order? If I wrote the code correctly all parts should disappear and reappearing seemingly instantaneously together.

Yea because i’m making trying to make an obby and one of the obstacles is the disappearing platform.
I want to make it so that if you step on a platform it disappears. I could make them individually but I don’t want a whole lot of parts in my workspace.

Sorry if it appeared twice

1 Like

Forget everything I previously had in this text. The issue isn’t anything I thought it was. This one script is still handling the previous block while you step on a new block. Roblox scripts can’t multi-thread. Put a print for when CanRun is out back to true, it doesn’t happen until the block is back, which means the new block can’t be enacted on until the block behind it is visible again.

So you’re either going to want to put a script in each individual block, or perhaps use collectionService which lets multiple tagged instances run off of one script and not interfere.

The collectionService is exactly what I was looking for, Thanks for the help!

1 Like

Glad to help! May I also add it is 10x easier to make a script with it for this scenario. Just have the function for when the part is touched, no loops needed, no GetChildren needed, no more redundancy…

Alright, I will definitely study up on it now, Thanks alot

One more question, would I be able to keep my script in the ServerScriptService?

Yes, since parts are server-sided, they can pick up on scripts in ServerScriptService as usual.