Unable to cast value to Object

So i tried to use tween to make it slowly transparency 1, But it says “Unable to cast value to Object”

What is this, And how do i fix it?

local TweenS = game:GetService("TweenService")

local TweenI = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local STWParts = script.Parent.ShadowTheWorld:GetChildren()

local STWAppear = TweenS:Create(STWParts, TweenI, {Transparency = 1})
local STWDisappear = TweenS:Create(STWParts, TweenI, {Transparency = 0})

The reason this doesn’t work is because you are attempting to tween a table, when instead you should be tweening the values inside the table. To do this, you’d get the parts using a for loop.

local TweenS = game:GetService("TweenService")

local TweenI = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local STWParts = script.Parent.ShadowTheWorld:GetChildren()

local function tweenParts(model, transparency)
    for _, part in pairs(model:GetChildren()) do
        if part:IsA("BasePart") then
            TweenS:Create(part, TweenI, {Transparency = transparency}):Play()
        end
    end
end  -- I Created a function that tweens the transparency of models

tweenParts(STWParts, 1) -- This is the STWAppear function 
tweenParts(STWParts, 0) -- This is the STWDisappear Function
2 Likes

Hm, Doesn’t seem to 100% work, Might be me doing something wrong?
https://gyazo.com/4890022d8aa976b16aa315da7a5e7b55

His script should work fine, you’re setting the second parameter to the target transparency on line 32 correct?

I’m doing exactly what his script is showing

local TweenS = game:GetService("TweenService")

local TweenI = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local STWParts = script.Parent.ShadowTheWorld:GetChildren()

local function tweenParts(model, transparency)
    for _,part in pairs(model:GetChildren()) do
        if part:IsA("BasePart") or part:IsA("UnionOperation") or part:IsA("MeshPart") then
            TweenS:Create(part, TweenI, {Transparency = transparency}):Play()
        end
    end
end 

Then i call it with “tweenParts(STWParts, 1)”

I apologize my script was incorrect because I called :GetChildren() Twice.

local TweenS = game:GetService("TweenService")

local TweenI = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local STWParts = script.Parent.ShadowTheWorld

local function tweenParts(model, transparency)
    for _,part in pairs(model:GetChildren()) do
        if part:IsA("BasePart") then
            TweenS:Create(part, TweenI, {Transparency = transparency}):Play()
        end
    end
end 

BTW :IsA(“BasePart”) Accounts for UnionOperations and MeshParts too :slight_smile:

2 Likes

Ohh! And i didn’t even see the :GetChildren() twice!
Thanks for the help!

1 Like