Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I make this transparency script shorter?

Asked by 2 years ago

The title says It all, how can I modify this script so Its more shorter, I don't want to copy and paste line 7 and 9 just to make the player Invisible and visible

Script:

local Troll = script.Parent
local Debounce = false

Troll.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    if not Debounce then
        Debounce = true
        Troll.Torso.Transparency = 1  ----Don't want to literally copy & paste this just to make all the bodyparts Invisible
        wait(3)
        Troll.Torso.Transparency = 0
        wait(1)
        Debounce = false
    end
end)

1 answer

Log in to vote
2
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

you can use for loops, loop through each body part of the person!

local Troll = script.Parent
local Debounce = false

Troll.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    if not Debounce then
        Debounce = true

        for _, Part in Troll:GetChildren() do
            if Part:IsA("BasePart") and Part.Name ~= "HumanoidRootPart"  then
                Part.Transparency = 1
            end
        end

        wait(3)

        for _, Part in Troll:GetChildren() do
            if Part:IsA("BasePart") and Part.Name ~= "HumanoidRootPart"  then
                Part.Transparency = 0
            end
        end

        wait(1)
        Debounce = false
    end
end)


but this script is too long so i'll create a function for that:

local Troll = script.Parent
local Debounce = false

local function SetTransparency(Transparency)
    for _, Part in Troll:GetChildren() do
        if Part:IsA("BasePart") and Part.Name ~= "HumanoidRootPart" then
            Part.Transparency = Transparency
        end
    end
end

Troll.Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    if not Debounce then
        Debounce = true

        SetTransparency(1)
        wait(3)
        SetTransparency(0)
        wait(1)
        Debounce = false
    end
end)

notice that i ignore HumanoidRootPart since that should always stay invisible.

Ad

Answer this question