How can I relate to all parts of the character at once?

Hello,

There is a character in my game. I’d like to use TweenService to make character transparency animated from 1 to 0 but i don’t want to make code somthing likie this:

goal = {}
goal.Head.Transparency = 0
goal.["Left Arm"].Transparency = 0
goal.["Left Leg"].Transparency = 0
--etc

How can I relate to all parts of the character at once?

Thanks in advance.

here we go bois

local plrs = game:GetService("Players")
local tween_service = game:GetService("TweenService")
local info = TweenInfo.new(3)
local goals = {
	Transparency = 1
}

plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		task.wait()
		
		for _, v in ipairs(char:GetDescendants()) do
			if v:IsA("BasePart") then
				tween_service:Create(v, info, goals):Play()
				end
		end	
	end)
end)

This would cause some errors as not all descendants of the character have a transparency property, should check the class of the object before tweening, or wrap it in a pcall

1 Like

oh yeah forgot that if statement 1 sec

It was about the character as a model, not player

the character there is a model. I was just providing an example. You can just take the for loop and it’ll work the same

The following code still does what you want, simply set char to your target character.

local tweens = game:GetService("TweenService")

local function tweenInstance(instance, bool)
	local transparency = if bool then 1 else 0
	local tween = tweens:Create(instance, TweenInfo.new(1, Enum.EasingStyle.Linear), {Transparency = 1})
	tween:Play()
end

local function toggleTransparency(character, bool) --Function requires character model.
	for _, child in ipairs(character:GetChildren()) do
		if child:IsA("BasePart") then
			tweenInstance(child, bool)
			if child.Name == "Head" then
				local face = child:FindFirstChild("face")
				if face then
					tweenInstance(face, bool)
				end
			end
		elseif child:IsA("Accessory") then
			local handle = child:FindFirstChild("Handle")
			if handle then
				tweenInstance(handle, bool)
			end
		elseif child:IsA("Tool") then
			local handle = child:FindFirstChild("Handle")
			if handle then
				tweenInstance(handle, bool)
			end
		end
	end
end

Here’s a script which will allow you to toggle a character’s visibility using tweens. The bool argument when set to true will make the character invisible, when false it’ll make the character visible.

The character’s limbs, accessories & tools are all tweened.

DRY DRY DRY DRY. It’s almost everywhere. Use pairs bro.

1 Like

Doesn’t ipairs do one thing at a time? While pairs does them all?

Sure ipairs is better than pairs, but there’s nothing wrong with pairs, you can use ipairs or pairs