Attempt to Index nil With 'TweenSize'

So I am trying to make a loading screen for my game, however sometimes when I run my game, “script.Parent:TweenSize” comes out as nil, and exclusively on line 25. I do not have a clue on how to fix this.

Here’s the script:

So I am trying to make a loading screen for my game, however sometimes when I run my game, "script.Parent:TweenSize" comes out as nil, and exclusively on line 25.  I do not have a clue on how to fix this.

local screen = math.random(1,3)
local NewTitle = script.Parent.Parent.Parent.Parent.Title

if screen == 1 then
 script.Parent.Parent.Parent.Parent.Enabled = true
 wait(2)

 script.Parent:TweenSize(UDim2.new(.3, 1, 1), "Out", "Linear", 2, true)
 wait(2)
 script.Parent:TweenSize(UDim2.new(.5, 1, 1), "Out", "Linear", 1, true)
 wait(3)
 script.Parent:TweenSize(UDim2.new(1, 1, 1), "Out", "Linear", 1, true)


 wait(1)
    script.Parent.Parent.Parent:Destroy()
 wait(1)
 script.Parent:TweenSize(UDim2.new(0, 0, 0), "Out", "Linear", 1, true)
end

if screen == 2 then
    script.Parent.Parent.Parent:Destroy()
 wait(2)

 script.Parent:TweenSize(UDim2.new(.1, 1, 1), "Out", "Linear", 3, true) -- LINE 25 (Error:Players.Eh_Canadian0.PlayerGui.Loading.Loading.Loading.LoadingTick.Handler:25: attempt to index nil with 'TweenSize')
 wait(1)
 script.Parent:TweenSize(UDim2.new(.4, 1, 1), "Out", "Linear", 2, true)
 wait(4)
 script.Parent:TweenSize(UDim2.new(.7, 1, 1), "Out", "Linear", 3, true)
 wait(1)
 script.Parent:TweenSize(UDim2.new(1, 1, 1), "Out", "Linear", 1, true)


 wait(1)
 script.Parent.Parent.Parent.Parent.Enabled = true
 wait(1)
 script.Parent:TweenSize(UDim2.new(0, 0, 0), "Out", "Linear", 1, true) 
end
if screen == 3 then
 script.Parent.Parent.Parent.Parent.Enabled = true
 wait(2)

 script.Parent:TweenSize(UDim2.new(.6, 1, 1), "Out", "Linear", 2, true)
 wait(2)
 script.Parent:TweenSize(UDim2.new(.8, 1, 1), "Out", "Linear", 3, true)
 wait(3)
 script.Parent:TweenSize(UDim2.new(1, 1, 1), "Out", "Linear", 1, true)

 script.Parent.Parent.Parent:Destroy()
end```

Could you add a print statement to check what script.Parent is referring to? It could be because the Parent is not valid (Or visible) to the script?

1 Like

Try doing

Enum.TweenSize
1 Like

So I put:

‘’‘lua
print (script.Parent.Name)
‘’’

in the script and the parent’s name came out just fine

wait the problem was because I did

lua script.Parent.Parent.Parent:Destroy()

in the middle of the script