Help with Loops(?)

Hello! Newbie scripter here.

I’m trying to make a platform minigame(just trying to learn scripting) where after you step on a platform, CanCollide turns off and the part becomes invisible.

All 25 of my platforms are named the same thing and when I step on one of them, the event doesn’t fire for that specific part.

I guess I could name each of the parts a different name and put scripts into each one of them, but I am looking for a more effective way to do such a task.

I was suggested to put them into a folder and use a loop(?)

This is the code I have so far.

game.Workspace.platform.Touched:Connect(function()
wait(2)
	game.Workspace.platform.CanCollide = false
	game.Workspace.platform.Transparency = 1
end)

I’m not sure what to do and would greatly appreciate some feedback. Thank you! :slight_smile:

3 Likes

Try put the script inside the block and make

script.Parent.Touched:Connect(function()
wait(2)
	game.Workspace.platform.CanCollide = false
	game.Workspace.platform.Transparency = 1
end)

Remember, if the platform is not a part then it won’t work. the .Touched event only works on part

2 Likes

Make sure each part’s CanTouch property is on. Also, can we see the parts assembled in the explorer tab?

1 Like

It works, because he said:

So works, the problem is that is making in the wrong part

same reply to @ItzMeZeus_IGotHacked

If you want to do the .Touched event all at once use for loops. Here it should work like this

for _, part in pairs(game.Workspace.platform:GetDescendants()) do -- Looping through the platform model
	if part:IsA("Part") then -- Checking if the part is a Part. If it is then
		part.Touched:Connect(function(hit) -- touching the part
			wait(2) -- waiting 2 seconds
			local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- getting player from the player service

			if player then -- if it's a player then
				part.CanCollide = false -- set the part cancollide to false
				part.Transparency = 1 -- set the part transparency to 1
			end
		end)
	end
end
1 Like

No, he want to make like the tile games, when touch, after 2 seconds the tile become invisible and impossible to touch it (cancollide false)

I don’t quite understand what he meant by that.

1 Like

It’s like a tile games, when you touch a tile, the tile become invisible, but the tile that become invisible is the wrong tile.

I still don’t get it. Do you mean the like his problem is that only one tile isn’t firing the event?

1 Like

No, I’ll explain better.

Like in parkours it has parts that when you touch, it disappear and you need to go fast to the next part.

It’s the same here, but the part that become invisible isn’t the part you touch, but yes a random part, because all parts have the same name.

Tile games are like some games in which you touch a part after a little moment you cant see or touch it. If you fall then you lose.

1 Like

This is the problem. Just because you name everything the same, doesn’t mean they connect the same event. You have to manually connect each event to the part through a loop.

1 Like

I already say a solution in first reply without using a loop

Only put the script inside all the parts

1 Like

You have a very unoptimized script. You should’ve known that more scripts will need more performance. Using a for loop saved that performance.

2 Likes

it works but if u want to control all parts at once then u can try mine

2 Likes

No, this is only if all the scripts are loops, because they only work when fired

What do you mean? You’re saying to put this script into every tile part. I’ve said this is unoptimized and bad for performance. For loop is much better in this case. We use the loop to connect the event.

2 Likes

But the scripts only are activated when the player touch a part, and when don’t touch a part the script is like being disabled.

You’re talking nonsense. The purpose of this loop is to connect the event to the part. Then, when Teh part is being connected to that event, it will listen to that event and fires when they get touched.