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