"Unable to cast value to Object" error thrown using TweenService, ModuleScripts

Hi, all. I am experimenting with ModuleScripts and am attempting to fade in two TextLabels (a disclaimer) as soon as the player loads in. I have set up a ModuleScript to handle text animations by returning a Tween. A LocalScript located in ReplicatedFirst handles the loading. However, when I load into the game, the LocalScript throws “Unable to cast value to Object” at line 15 of the modulescript (Client - TextAnim:15) and I’m not sure why.

Here is the ModuleScript (“TextAnim”)

local TS = game:GetService("TweenService")
type TextObject = TextLabel | TextButton | TextBox

local UIAnim: {[string]: any} = {
	DefaultTweenInfo = TweenInfo.new(2)
}

--- Returns a tween that will make the text of a TextObject fade out
UIAnim.TextFade = function(Obj: TextObject, TransparencyGoal, CustomTweenInfo) : Tween
	local goal = {
		TextTransparency = TransparencyGoal
	}
	local tween
	if CustomTweenInfo then
		tween = TS:Create(Obj, CustomTweenInfo, goal)
	else
		tween = TS:Create(Obj, UIAnim.DefaultTweenInfo or CustomTweenInfo, goal)
	end
	return tween
end

..and here is the LocalScript (“Loader”)


----------------------
local SkipDisclaimer = false
----------------------

-- Game Services
local P = game:GetService("Players")
local TS = game:GetService("TweenService")

-- Required Modules
local TextAnim = require(script.TextAnim)

-- Instances
local plr = P.LocalPlayer
local PlayerGui = plr.PlayerGui
--[[
local DisplayGui = PlayerGui.DisplayGui or PlayerGui:WaitForChild("DisplayGui") -- the displaygui the player actually sees
]]

local DisclaimerGui = script.Menu

-- Script
if not SkipDisclaimer then
	plr.CharacterAdded:Wait()
	DisclaimerGui.Parent = PlayerGui
	for _, TextObj in DisclaimerGui:GetChildren() do
		print(TextObj.ClassName)
		local tween = TextAnim:TextFade(TextObj, TweenInfo.new(1), 0)
		tween:Play()
	end
end

I tried resolving this by setting the types as well as checking the classnames of the objects being put into TS:Create(), but with no success. I could not find any other forum posts with this specific issue.

Any assistance would be greatly appreciated!

You are calling the function using syntax sugar (TextAnim:TextFade(...)), which will implicitly pass TextAnim itself as the first argument to your function. Do not use a colon to call the function if you do not want to pass the table itself as the first argument to the function. You can fix it by calling the function the normal way:

local tween = TextAnim.TextFade(TextObj, TweenInfo.new(1), 0)
1 Like

Don’t know how I missed that. This works perfectly now–thank you.