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:
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
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.