Cloak script not working as expected

Hey all,

In the place I’m building there are two teams and vehicles. If you press the key to cloak your vehicle, the parts should all disappear for the other team, whereas a billboard gui with the text “cloaked” will appear over your vehicle for players on your team.

The transparency part is not working as expected.

Expectation:

Reality:

Code:

					else if team == blue then
							for _, bruh in pairs(teams['Blue']:GetPlayers()) do
								engine.Transparency = 1
								local children = bodykit:GetChildren()
								for bruh, children in pairs(children) do
									children.Transparency = 0.1
									wait(0.1)
									children.Transparency = 0.2
									wait(0.1)
									children.Transparency = 0.3
									wait(0.1)
									children.Transparency = 0.4
									wait(0.1)
									children.Transparency = 0.5
									wait(0.1)
									children.Transparency = 0.6
									wait(0.1)
									children.Transparency = 0.7
									wait(0.1)
									children.Transparency = 0.8
									wait(0.1)
									children.Transparency = 0.9
									wait(0.1)
									children.Transparency = 1
									wait(5)
									children.Transparency = 0
								end
								engine.Transparency = 0
							end
4 Likes

You’re basically having the loop wait to get each child by using wait() in the loop. In addition, I would suggest using TweenService to animate the transparency fade.

How can I just get it to make all the children transparent at once instead of doing it one by one

You can create a Tween with the Instance being ‘children’ and play it, and the children should all be tweened to become transparent simultaneously.

1 Like

First of all, You should follow the method that @PineappleSmoothie12 mentioned which is using TweenService to make all your parts transparent.

Secondly, whenever you run a loop it will NOT run all at once. Meaning that if you try to deliver 10 cookies at 10 different locations, you will not be able to deliver all of the cookies at the same time until you deliver them in a certain order.

Finally, your code should also use Coroutines.

This is what your code should look like:

local TweenService = game:GetService("TweenService")

for _, Object in pairs(children) do
    coroutine.resume(coroutine.create(function()
        TweenService:Create(Object, TweenInfo.new(1), {Transparency = 1}):Play()
        wait(1)
        TweenService:Create(Object, TweenInfo.new(0), {Transparency = 1}):Play()
    end)
end
1 Like

Is this the sort of effect you are looking for? https://streamable.com/to3ant

3 Likes

ok, so I was going to mark this as the solution when i finished the script, but that hasn’t been going too well.

Everything you wrote worked great, but there was more to it.

If i was to exit the plane, a script that is a parent to the localscript of the plane would fire because the seatweld for the player would be destroyed. This stops the plane and removes the gui, and disables the localscript. Problem is, if the player exited before the cloak was over, they would be invisible until they die because the script gets disabled, which is a problem in a combat game.

To circumvent this i fired a server event when the player pressed the cloak button, and the event is picked up by the parent script, and connects() when the seatweld:Removed(). But because the local script made the player invisible, the script is only making the player visible again to only the server, not the players.

So what do i do?

I made another frickin’ remote event and when the seatweld:Removed() and the other event is connected() the script fires the event to a child local script that connects() this and makes the player invisible.
As you can already guess the local script is not receiving the event, even though print statement in script says it is firing.

Hierarchy:
image

Script:

script.Parent.ChildRemoved:connect(function(child)
	if child:IsA("Weld") and child.Name == "SeatWeld" then
		local db = false
		script.PlrLeft.OnServerEvent:Connect(function(player)
			if not db then
				db = true
				script.PlrLeft.Checker:FireClient(player)
				print("nut")
				wait(0.01)
				db = false
			end
		end)
		newgui:remove()
		wait()
		newgui = gui:Clone()
		script.Parent.BodyGyro.MaxTorque = Vector3.new(0,0,0)
		script.Parent.BodyVelocity.MaxForce = Vector3.new(0,0,0)
	end	
end)

Transparency Checker:

local e = script.Parent.PlrLeft.Checker

local db = false
function one()
	print("bruh moment")
	if not db then
		db = true
		local v1 = workspace:FindFirstChild(player.Name)
		for v2,v in pairs(v1:GetChildren()) do
			print(v)
			if v:IsA("Part") then
				v.Transparency = 0
			end
			if v:IsA("Accessory") then
				v.Handle.Transparency = 0
			end
		end
		wait(0.1)
		db = false
	end
end	
e.OnClientEvent:Connect(one)

Is there a way to make this less complicated?

p.s. @hamsterloverboy3 that video isn’t what I meant but is actually really neat, thanks nonetheless!

If you wanna make it somewhat organized, make two scripts, one script the controller, and the other the cloak mechanism. The controller will control all the entering and leaving. If the local script detects player wanting to cloak it signals to the script to send an event to the cloak script. The cloak script will wait to check if player is still welded and will invis the player and the vehicle. If the player leaves the cloak script will see that no one is welded to the script and will turn him visible again.

You can also try to make the local script fire the event and delete the GUI first and make the controller script then receive that event if the player leaves

it cant do that beacuse it stops working because there is no local player for that local script

I’m not sure scripts can make a single player invisible, because that would just make it invisible to the server. And the plane controller is the local script, the parent script just handles giving the local script and taking it away when the player gets in the seat. I could just do this but in the local script which is already what I am doing.

1 Like

You can try to find the certain player on the seat by looking at the parent of the seat weld and making that player invisible perhaps?

So I was on the right track, the solution was that the localscript never ran because it never had any legs to run on. what i mean is that in order for a local script to run it needs something player-objecty like a tool or starterplayerscripts. It never caught the event because it never even ran in the first place.

The solution was just moving the script to starterplayerscripts.

@Cosmental
And now, a week late, for your own viewing pleasure:

2 Likes

Looks great!

Just a heads up, you may want to remove the players face to fully give off an effect and then re-add the players face when they exit or when the ship becomes visible again.

You may also want to allow the client to see the ship but keep them hidden to the server. (Its up to you!)

i was gonna keep the player and the plane visible to the cloaked player’s team, but invisible to the opposing team. I didn’t remove the players face because i don’t know how to do that and i just decided that planes fly at a distance/height, so the face won’t easily be spotted.

Unless you know how to remove and readd the face?

also why does the glass look like that after i cloak the plane? I named the glass “Glass” and in the script i search for “Glass” inside the bodykit and change the transparency to 0.4, but it never seems to find it.

Is the glass instanced locally?

I sure do!
In your script write:

local Character = ThePlayersCharacter
local Head = Character:WaitForChild("Head")
local Face = Head:FindFirstChildOfClass("Decal")

if Face then
    -- Your code here
end
1 Like

how do i remove the face and re-add it though?

wdym?

You could make a clone of the face you just deleted and hide it somewhere and if the player wants the face back you could probably maybe just parent it to the face.

There is another solution to this with load character and getting the decal but this is all I know right now.

Well I’d suggest syncing the face up with the transparency of the parts. The face is a decal inside the Head called face. Decals have a transparency property. It naturally makes sense to also make the face’s transparency fade just like the other parts.

ok, so that worked.

										for children, e in pairs(bodykit:GetChildren()) do
											if e.Name == "Glass" then
												e.Transparency = 0.4
											end
										end
									end))
								end
							end

no idea why this code is not working…
if i tell it to print e before making the glass transparent, it prints glass (so it is finding the single peice of glass) 20 times, which i guess is the amount of parts in bodykit.