Unable to cast value to Object

Hello! I am trying to add a tween for a GUI to smoothly open and close. So far, it has gone successful besides the fact that once it gets to the part when it has to make the content in the GUI visible. It tells me “Unable to cast value to Object”. Help would be appreciated. Thanks!

local Panel = script.Parent
local Cooldown = false
local Items = {}

function OpenPanel()
	Cooldown = true
	Panel.Visible = true
	TweenService:Create(Panel,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{Size = PanelSize}):Play()
	wait(0.5)
	for _, Object in pairs(Items) do
		if Object[1]:IsA("Frame") then
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{BackgroundTransparency = Object[2]}):Play()
		elseif Object:IsA("TextButton") or Object:IsA("TextLabel") or Object:IsA("TextBox") then	
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{TextTransparency = Object[2],BackgroundTransparency = Object[3]}):Play()
		elseif Object:IsA("ScrollingFrame") then
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{ScrollBarImageTransparency = Object[2]}):Play()
		elseif Object:IsA("ImageLabel") then
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{ImageTransparency = Object[2]}):Play()
		end
	end
	wait(0.5)
	table.clear(Items)
	Cooldown = false
end
1 Like

Also, This is a LocalScript if that matters. It’s also only apart of the script since the rest does not do anything to help.

You can’t tween the size and position of a GUI object using TweenService. You have to use a different method

1 Like

You can tween the property of something if it’s a number, the main issue is why it’s not casting the value to the property of the object.

you certainly can, my games are covered in stuff like this
TS:Create(frame, tweenInfo, {Position = UDim2.new(0.5,0,0.975,0)}):Play()
the UI-exclusive solutions are derivative of TweenService, are redundant in my opinion and should be superseded by TweenService directly. Personally I don’t know why they’ve yet to deprecate TweenPosition.

1 Like

Can you share what Object[2], Object[3] etc. are? They may not be a datatype castable to the property you’re assigning.

1 Like

As I believe you can see, They are in tables, but the actual things themselves are number values on things like the text transparency and background transparency.

Ah, my bad. I tried tweening a GUI object once and failed, then I never looked back at trying again, because I found those tweening functions.

2 Likes

I will also share the actual Close function if it helps.

function ClosePanel()
	Cooldown = true
	for _, Object in pairs(Panel:GetDescendants()) do
		if Object:IsA("Frame") then
			table.insert(Items,{Object,Object.BackgroundTransparency})
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{BackgroundTransparency = 1}):Play()
		elseif Object:IsA("TextButton") or Object:IsA("TextLabel") or Object:IsA("TextBox") then
			table.insert(Items,{Object,Object.TextTransparency,Object.BackgroundTransparency})
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{TextTransparency = 1,BackgroundTransparency = 1}):Play()
		elseif Object:IsA("ScrollingFrame") then
			table.insert(Items,{Object,Object.ScrollBarImageTransparency})
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{ScrollBarImageTransparency = 1}):Play()
		elseif Object:IsA("ImageLabel") then
			table.insert(Items,{Object,Object.ImageTransparency})
			TweenService:Create(Object,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{ImageTransparency = 1}):Play()
		end
	end
	wait(0.5)
	TweenService:Create(Panel,TweenInfo.new(0.5,Enum.EasingStyle.Sine),{Size = UDim2.new(0,0,0,0)}):Play()
	wait(0.5)
	Panel.Visible = false
	Cooldown = false
end

Shouldn’t it be TweenService:Create(Object[1],...)?

“Unable to cast value to object”, well Object is a table value but the first parameter of TweenService:Create should be an Instance/object

4 Likes

I see, you’re using index notation which is what confused me. Only your first conditional (IsA frame) is referencing Object[1], the rest reference the table directly, which will be producing the error you’re calling TweenService on a table. I would recommend moving to dictionary notation for clarity. I’ll reformat your first conditional (frame) in both to show what I mean:

Open

    for _, Object in pairs(Items) do
        if Object.Instance:IsA("Frame") then
            TweenService:Create(Object.Instance, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {BackgroundTransparency = Object.BackgroundTransparency}):Play()
        else--[...]

Close

    for k, Object in pairs(Panel:GetDescendants()) do
        if Object.Instance:IsA("Frame") then
            Items[k] = {Instance = Object, BackgroundTransparency = Object.BackgroundTransparency}
            TweenService:Create(Object, TweenInfo.new(0.5, Enum.EasingStyle.Sine), {BackgroundTransparency = 1}):Play()
        else--[...]
1 Like

That actually worked, I guess I forgot to put that. Thanks!

Yeah, sorry about that, my code can be a little messy sometimes.