Making lots of objects visible at once?

Hi!

So I have a marble track game and I want to have a secret track that is only visible when a player finds it, to the player themselves. Ideally I was hoping to have a sort of path-building effect where segments of the track would become visible one at a time as the player rolls along, but I could not find a way to get the collection service to work in tandem to make multiple parts fade in at once. It would have to wait for the first part to become visible before starting the next. I also tried putting it in a model and doing a “for each” to each child, but that did the same thing.

I’ll settle for the entire track becoming visible at once, but the collection service is not working as I thought it could. It picks one random part in the track to turn visible and none of the others.

local player = game.Players.LocalPlayer
local CollectionService = game:GetService("CollectionService")
local visiTrigger = game.Workspace.visTrigger
local transVal = 1
local deb = false

local function makeVisi(visi)
	while true do
		visi.Transparency = transVal
		wait()
	end
end

visiTrigger.Touched:Connect(function()
	if deb == false then
		deb = true
		for i = 1,5,1 do
			transVal = transVal - .2
			wait()
		end
	end
end)


for _, trig in pairs(CollectionService:GetTagged("visi")) do
	makeVisi(trig)
	--print (trig.Name)
end

How do I go about making the transparency fade apply to all the parts at the same time with one script? I don’t want to bog down my game either with a lot of scripts because the transparency changes too late and you lose the effect.

1 Like

its because the while true do loop inside the makeVisi function is yielding the for loop, you can avoid this by calling the function inside a new thread with task.spawn or coroutines

local player = game.Players.LocalPlayer
local CollectionService = game:GetService("CollectionService")
local visiTrigger = game.Workspace.visTrigger
local transVal = 1
local deb = false

local function makeVisi(visi)
	while true do
		visi.Transparency = transVal
		wait()
	end
end

visiTrigger.Touched:Connect(function()
	if deb == false then
		deb = true
		for i = 1,5,1 do
			transVal = transVal - .2
			wait()
		end
	end
end)


for _, trig in pairs(CollectionService:GetTagged("visi")) do
	task.spawn(function()
        makeVisi(trig)
	    print(trig.Name)
    end)
end
1 Like

How would I go about putting this into a coroutine? Do I put the coroutine in the function that is applied to each part with the collection service, or do I put it to the coroutine.create function?

local player = game.Players.LocalPlayer
local CollectionService = game:GetService("CollectionService")
local visiTrigger = game.Workspace.visTrigger
local transVal = 1
local deb = false

local thread = coroutine.create(function(visi)
	visi.Transparency = 0
end)

local function makeVisi(visi)
	coroutine.resume(thread)
end

visiTrigger.Touched:Connect(function()
	for _, trig in pairs(CollectionService:GetTagged("visi")) do
		makeVisi(trig)
		--print (trig.Name)
	end
end)

1 Like

Do you absolutely have to use collection service. I have no experience with it, but here is what I think is the effect you’re trying to achieve, without using collection service.

I tested it with multiple players, and only the player who touches the trigger will see track.

If you want I can explain in detail.

Yes that is what I want to do. How did you do that?

1 Like

All of the secret track belongs to the folder “Secret Track”
image
This way we can iterate through the folders children, setting their transparency.

Now there are two scripts involved. A server script, called “TriggerHandler” and a local script called “Secret Track”

TriggerHandler
This script is a server script, and is the child of the part “Trigger”, which is the semi transparent part you see in the video.
image

Here is the script in full.

Really, all it does is detect when the part is touched, then it finds which player touched it (line 12).
Here’s the crucial part, I used a remote event to allow this server script to communicate with the client script. See line 15.

Here is the documentation for remote events: Remote Events and Callbacks | Documentation - Roblox Creator Hub

SecretTrack
This is a client script stored in starterPlayerScripts. Its role is to listen for that remote event to be fired, and when it does fire, set the tracks transparency.
image

I called the debounce variable “alreadyVisible” (I assume your “deb” is a debounce")

Line 30 states that when the remote event is recieved on the client, run that function.
Line 18 is just another way of handling a debounce.

Then I do a for loop which is just the same as yours, but for each iteration of that for loop, I run another for loop which is in the function “setTrackTransparency”

The loop inside of set track transparency will iterate throughout the folders children which stores the secret track, setting each childs transparency to the transVal variable. And its complete.

Conclusion
2 important things to note.

Originally, I put the client script in startercharacterscripts. Every time the player dies and there character respawns, they would get a new client script. As whatever is in startercharacterscrcipts replicates into characters when they spawn in the world. However, this would mean that the line


would run multiple times for each player. (for every time they died)
You can still do it this way, as long as you disconnect the event when the player dies.

Additionally, you may want to use multithreading for the for loop in remoteEventRecieved (lines 21 to 26). You can use co-routines like @CZXPEK suggested, or some other method such as the older spawn() function.

Here is the file available for you to download:
SecretTrack Marble.rbxl (47.2 KB)

Any other questions feel free to ask, I dont have time to proof read this so any unclear areas or wrong areas just ask me about them.

1 Like

It worked! Thank you so much! I’ve been trying to figure this out for a long while so thank you for all the advice

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.