TweenService error on my Gui script

Hello, I have two viewport frames and I want to make a script that I can move around them and select them with these:


Looks squished because I took it in Studio.

I made my script but it gives an error, I couldn’t really find why it happens. Also I need to figure out a soluion for how can I hide them when they got out of the Gui frame in the middle so I hope you can help with it too.

  • My script:
--Definitions
local OutFrame = script.Parent
local LeftArrow = OutFrame:WaitForChild("LeftArrow")
local RightArrow = OutFrame:WaitForChild("RightArrow")
local VPFrameFolder = OutFrame:WaitForChild("ViewportFrames")
local allVPs = VPFrameFolder:GetChildren()
local tweenService = game:GetService("TweenService")
local inFramePos = UDim2.new{0.5,0,0.647,0}
local outFramePosLeft = UDim2.new(0, 0,0.363, 0)
local outFramePosRight = UDim2.new(0.639,0,0.363,0) 
--Indexes
local index = 0
local indexMin = 0
local indexMax = 1
--IndexRename
for i, value in pairs(allVPs) do
	value.Name = (tostring(i) .. " " .. value.Name)
end
--Tweens
local function leftSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosLeft})
	tween:Play()
	wait(1)
end

local function rightSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end

local function toCenterSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end
--Functions
local function indexCal(buttonname)
	if type(buttonname) ~= 'string' then
		assert(false, "Put a string here.")
	elseif buttonname == "Right" then
		index = index+1
		if index > indexMax then
			index = indexMin
		end
	elseif buttonname == "Left" then
		index = index - 1
		if index < indexMin then
			index = indexMax
		end
	end
end

local function lookForGui(index)
	for _, v in pairs(allVPs)do
		local stringName = v.Name
		local stringNameAll = string.split(stringName, " ")
		for _, v in pairs (stringNameAll) do
			if v == index then
				return v
			end
		end
	end
end

local function calculatePreviousLeft(index)
	if index-1 >= indexMin then return index-1 else return indexMin end
end

local function calculatePreviousRight(index)
	if index+1 <= indexMax then return index+1 else return indexMax end
end

local function onRightButtonClick()
	indexCal("Right")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousRight(index))
	rightSwipe(previousGui)
	toCenterSwipe(NextGui)
end

local function onLeftButtonClick()
	indexCal("Left")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	leftSwipe(previousGui)
	toCenterSwipe(NextGui)
end
--Events
RightArrow.MouseButton1Click:Connect(onRightButtonClick())
LeftArrow.MouseButton1Click:Connect(onLeftButtonClick())

Error:
image

Topic still up. Also is it because of the first TweenCreate functions are referring to frame but it doesn’t exist? It shall exist, I’m not running it with “frame”, just defining.

I updated the title, hope it’s more clear now.

Maybe the instance you’re trying to tween is nil?

That’s what error says. I’m pretty sure these are not nil, I remember myself printing these. But I’ll try again and inform.

You’re trying to tween frame but you never declared a variable named frame. So it’s nil.

But I defined the variable and with the behaviors of functions, doesn’t it need to run when I call it? And I’m passing a frame with argument.

The problem is you’re not passing a frame through the functions. It seems like you’re passing nothing. Make sure there’s a frame before firing the functions by doing a little debugging by adding a print(whateveryou'retryingtopass) right before you fire the functions to find out where and what has gone wrong. (I recommend you make the prints a bit different so you can distinguish the functions from one another to find out where exactly it all goes wrong)

I commented tweening parts and this is what it said:


There’s the error and my two changed functions. Probably these ones caused it because I don’t have any more connects. Only these.

When you connect a function to an event like that, you can’t have () behind it. This fires the function, which is not what you want. You want the function to fire on-event.

RightArrow.MouseButton1Click:Connect(onRightButtonClick)
LeftArrow.MouseButton1Click:Connect(onLeftButtonClick)
Extra

There are two methods AFAIK to connect a function with an event.

Method 1:

game.Players.PlayerAdded:Connect(function(Player)
-- code here
end)

Method 2:

local function OnPlayerAdded(Player)
-- code here
end
 
game.Players.PlayerAdded:Connect(OnPlayerAdded)
1 Like

Update:

I updated the code:

--Definitions
local OutFrame = script.Parent
local LeftArrow = OutFrame:WaitForChild("LeftArrow")
local RightArrow = OutFrame:WaitForChild("RightArrow")
local VPFrameFolder = OutFrame:WaitForChild("ViewportFrames")
local allVPs = VPFrameFolder:GetChildren()
local tweenService = game:GetService("TweenService")
local inFramePos = UDim2.new{0.5,0,0.647,0}
local outFramePosLeft = UDim2.new(0, 0,0.363, 0)
local outFramePosRight = UDim2.new(0.639,0,0.363,0) 
--Indexes
local index = 1
local indexMin = 1
local indexMax = 2
--IndexRename
for i, value in pairs(allVPs) do
	value.Name = (tostring(i) .. " " .. value.Name)
end
--Tweens
local function leftSwipe(frame)
	if not frame.Visible then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosLeft})
	tween:Play()
	wait(1)
end

local function rightSwipe(frame)
	if not frame.Visible then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end

local function toCenterSwipe(frame)
	if not frame.Visible then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end
--Functions
local function indexCal(buttonname)
	if type(buttonname) ~= 'string' then
		assert(false, "Put a string here.")
	elseif buttonname == "Right" then
		index = index + 1
		if index > indexMax then
			index = indexMax
		end
	elseif buttonname == "Left" then
		index = index - 1
		if index < indexMin then
			index = indexMin
		end
	end
end

local function lookForGui(index0)
	index0 = tostring(index0)
	for _, v in pairs(allVPs)do
		local stringName = v.Name
		local stringNameAll = string.split(stringName, " ")
		for i, v1 in pairs (stringNameAll) do
			if v1 == index0 then
				local selectedGuiName = stringNameAll[i+1]
				selectedGuiName = (i .. " " .. selectedGuiName)
				for _, v2 in pairs (allVPs) do
					if v2.Name == selectedGuiName then
						print(v2.Name)
						return v2
					else
						selectedGuiName = nil
					end
				end
			end
		end
	end
end

local function calculatePreviousLeft(index0)
	if index0-1 >= indexMin then return index0-1 else return indexMin end
end

local function calculatePreviousRight(index0)
	if index0+1 <= indexMax then return index0+1 else return indexMax end
end

local function onRightButtonClick()
	indexCal("Right")
	print(index)
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousRight(index))
	rightSwipe(previousGui)
	toCenterSwipe(NextGui)
end

local function onLeftButtonClick()
	indexCal("Left")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	leftSwipe(previousGui)
	toCenterSwipe(NextGui)
end
--Events
RightArrow.MouseButton1Click:Connect(onRightButtonClick)
LeftArrow.MouseButton1Click:Connect(onLeftButtonClick)

And with that print, it successifully prints the first one, ClassicSword. When it comes to LongSword, it just prints indexname.

Output:


(Prints ClassicSword every time I press LeftButton which is normal, minimum is 1 and maximum is 2.)
(It needs to print LongSword Gui’s name but it doesn’t. That’s the problem.)

How can I make it see LongSword Viewport Frame?

I updated the script again.

I added the index numbers on the name of Gui’s and removed the script.
Also I found out that I need to reset these values after each loop so I added nil’s.

--Definitions
local OutFrame = script.Parent
local LeftArrow = OutFrame:WaitForChild("LeftArrow")
local RightArrow = OutFrame:WaitForChild("RightArrow")
local VPFrameFolder = OutFrame:WaitForChild("ViewportFrames")
local allVPs = VPFrameFolder:GetChildren()
local tweenService = game:GetService("TweenService")
local inFramePos = UDim2.new{0.5,0,0.647,0}
local outFramePosLeft = UDim2.new(0, 0,0.363, 0)
local outFramePosRight = UDim2.new(0.639,0,0.363,0) 
--Indexes
local index = 1
local indexMin = 1
local indexMax = 2
--Tweens
local function leftSwipe(frame)
	if not frame.Visible then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosLeft})
	tween:Play()
	wait(1)
end

local function rightSwipe(frame)
	if not frame.Visible then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end

local function toCenterSwipe(frame)
	if not frame.Visible then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end
--Functions
local function indexCal(buttonname)
	if type(buttonname) ~= 'string' then
		assert(false, "Put a string here.")
	elseif buttonname == "Right" then
		index = index + 1
		if index > indexMax then
			index = indexMax
		end
	elseif buttonname == "Left" then
		index = index - 1
		if index < indexMin then
			index = indexMin
		end
	end
end

local function lookForGui(index0)
	index0 = tostring(index0)
	for _, v in pairs(allVPs)do
		local stringName = v.Name
		local stringNameAll = string.split(stringName," ")
		for i, v1 in pairs (stringNameAll) do
			print(v1)
			if v1 == index0 then
				local selectedGuiName = stringNameAll[i+1]
				local previous = stringNameAll[i]
				selectedGuiName = (previous.." "..selectedGuiName)
				print("reached")
				--print(selectedGuiName)
				for _, v2 in pairs (allVPs) do
					if v2.Name == selectedGuiName then
						--print(v2.Name)
						selectedGuiName = nil
						stringName = nil
						stringNameAll = nil
						previous = nil
						return v2
					else
						selectedGuiName = nil
						stringName = nil
						stringNameAll = nil
						previous = nil
					end
				end
			else
				stringName = nil
				stringNameAll = nil
			end
		end
	end
end

local function calculatePreviousLeft(index0)
	if index0-1 >= indexMin then return index0-1 else return indexMin end
end

local function calculatePreviousRight(index0)
	if index0+1 <= indexMax then return index0+1 else return indexMax end
end

local function onRightButtonClick()
	indexCal("Right")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousRight(index))
	rightSwipe(previousGui)
	toCenterSwipe(NextGui)
end

local function onLeftButtonClick()
	indexCal("Left")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	leftSwipe(previousGui)
	toCenterSwipe(NextGui)
end
--Events
RightArrow.MouseButton1Click:Connect(onRightButtonClick)
LeftArrow.MouseButton1Click:Connect(onLeftButtonClick)

Apperantly output became more meaningful.

  • This is what happens when I click on LeftButton (activates ClassicSword viewport frame, also it works)
    image
    So we can say that it shouldn’t print Gui’s name, but it shall print “reached”. And this is pretty normal considering index (after string.split, first object of the string) triggers the first if and it selects the next object without needing to run the loop again. It’ll just return false on second loop anyways.

  • This is what happens when I click RightButton, which’ll trigger LongSword viewport frame.


    So we can see that it prints 2 times like other one but it still prints LongSword after “reached” which we don’t want since reached will be the last thing on that loop. That probably overwrites.

Known things:

  • When I change first numbers of both of the Gui’s, second one breaks. So indexes are working correctly.
  • I tested with breakpoints and got these resultss:
    – Index scripts are correct
    – All values are like expected.
    – Real problem is,

    I used breakpoints and learnt that it runs 1 perfectly.
    It runs 2 too and v2 starts with “1 ClassicSword” as expected.
    But script never goes to points which are marked with 3. It doesn’t even go to end.

Worth noting that, I was editing positions and I needed to put them back in folder. When I put LongSword first, ClassicSword one stops working.

I’ve updated the topic. If someone can help me, it’d be really appreciated.

Update:


It said this but I’m not exactly sure what does it mean.

From the code you gave us, at line 74 you return a value, however in the else part of your if statement from line 68 (Else starts at line 76) you don’t have it returning anything at all.
Perhaps because it never finds the object you’re lookin for in variable allVPs.

line 6 - local allVPs = VPFrameFolder:GetChildren()

Basically, function lookForGui is being used to retrieve a value for somewhere in your code but it gets to the point where it isn’t returning anything. Therefore adding a return nil to the end of the function and verifying wherever you used that returned value in your code isn’t nil should fix the message.

The function quits out if you return so it will only go to return nil if it can’t find v2 == selectedGuiName.

is it possible that you’re calling for children too fast at the begining of the script, it’s easy to get ahead of yourself and forget that things take time to load, believe me, I’ve had it happen to myself.

BTW,
if selectedGuiName ~= v2 than selectedGuiName gets set to nil if allVPs still contains what you’re looking for you’ll never find it.
This may explain the “W016: Function lookForGui can implicitly return no values even though there’s an explicit return at line 77; add explicit empty return to silence”

Tried but didn’t work.

They are being checked when buttonpressed so I don’t think that will happen.

I didn’t understand that.

By the way, I tried this script:

--Definitions
local OutFrame = script.Parent
local LeftArrow = OutFrame:WaitForChild("LeftArrow")
local RightArrow = OutFrame:WaitForChild("RightArrow")
local VPFrameFolder = OutFrame:WaitForChild("ViewportFrames")
local allVPs = VPFrameFolder:GetChildren()
local tweenService = game:GetService("TweenService")
local inFramePos = UDim2.new(0.5,0,0.647,0)
local outFramePosLeft = UDim2.new(0, 0,0.363, 0)
local outFramePosRight = UDim2.new(0.639,0,0.363,0) 
--Indexes
local index = 0
local indexMin = 0
local indexMax = 1
--IndexRename
for i, value in pairs(allVPs) do
	value.Name = (tostring(i) .. " " .. value.Name)
end
--Tweens
local function leftSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosLeft})
	tween:Play()
	wait(1)
end

local function rightSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end

local function toCenterSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end
--Functions
local function indexCal(buttonname)
	if type(buttonname) ~= 'string' then
		assert(false, "Put a string here.")
	elseif buttonname == "Right" then
		index = index+1
		if index > indexMax then
			index = indexMin
		end
	elseif buttonname == "Left" then
		index = index - 1
		if index < indexMin then
			index = indexMax
		end
	end
end

local function lookForGui(index)
    for _, v in pairs(allVPs)do
        local stringName = v.Name
        local stringNameAll = string.split(stringName, " ")
        for _, v1 in pairs (stringNameAll) do
            if v1 == index then
                print(v1)
                return v
			else
				stringName = nil
				stringNameAll = nil
            end
        end
    end
    print(nil)
    return nil
end

local function calculatePreviousLeft(index)
	if index-1 >= indexMin then return index-1 else return indexMin end
end

local function calculatePreviousRight(index)
	if index+1 <= indexMax then return index+1 else return indexMax end
end

local function onRightButtonClick()
	indexCal("Right")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	if previousGui then
		rightSwipe(previousGui)
	end
	if NextGui then
		toCenterSwipe(NextGui)
	end
end

local function onLeftButtonClick()
	indexCal("Left")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	if previousGui then
		leftSwipe(previousGui)
	end
	if NextGui then
		toCenterSwipe(NextGui)
	end
end
--Events
RightArrow.MouseButton1Click:Connect(onRightButtonClick)
LeftArrow.MouseButton1Click:Connect(onLeftButtonClick)

It prints nil on both buttons.

By the way, these breakpoints never get triggered.
image

Seems like fixed.

--Definitions
local OutFrame = script.Parent
local LeftArrow = OutFrame:WaitForChild("LeftArrow")
local RightArrow = OutFrame:WaitForChild("RightArrow")
local VPFrameFolder = OutFrame:WaitForChild("ViewportFrames")
local allVPs = VPFrameFolder:GetChildren()
local tweenService = game:GetService("TweenService")
local inFramePos = UDim2.new(0.5,0,0.647,0)
local outFramePosLeft = UDim2.new(0, 0,0.363, 0)
local outFramePosRight = UDim2.new(0.639,0,0.363,0) 
--Indexes
local index = 1
local indexMin = 1
local indexMax = 2
--Tweens
local function leftSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosLeft})
	tween:Play()
	wait(1)
end

local function rightSwipe(frame)
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end

local function toCenterSwipe(frame)
	if frame.Visible == false then frame.Visible = true end
	local tweenInfo = TweenInfo.new(1,Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
	local tween = tweenService:Create(frame, tweenInfo, {Position = outFramePosRight})
	tween:Play()
end
--Functions
local function indexCal(buttonname)
	if type(buttonname) ~= 'string' then
		assert(false, "Put a string here.")
	elseif buttonname == "Right" then
		index = index+1
		if index > indexMax then
			index = indexMin
		end
	elseif buttonname == "Left" then
		index = index - 1
		if index < indexMin then
			index = indexMax
		end
	end
end

local function lookForGui(index)
	index = tostring(index)
    for _, v in pairs(allVPs)do
        local stringName = v.Name
        local stringNameAll = string.split(stringName, " ")
        for _, v1 in pairs (stringNameAll) do
            if v1 == index then
                print(v1)
                return v
			else
				stringName = nil
				stringNameAll = nil
            end
        end
    end
    print(nil)
    return nil
end

local function calculatePreviousLeft(index)
	if index-1 >= indexMin then return index-1 else return indexMax end
end

local function calculatePreviousRight(index)
	if index+1 <= indexMax then return index+1 else return indexMin end
end

local function onRightButtonClick()
	indexCal("Right")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	if previousGui then
		rightSwipe(previousGui)
	end
	if NextGui then
		toCenterSwipe(NextGui)
	end
end

local function onLeftButtonClick()
	indexCal("Left")
	local NextGui = lookForGui(index)
	local previousGui = lookForGui(calculatePreviousLeft(index))
	if previousGui then
		leftSwipe(previousGui)
	end
	if NextGui then
		toCenterSwipe(NextGui)
	end
end
--Events
RightArrow.MouseButton1Click:Connect(onRightButtonClick)
LeftArrow.MouseButton1Click:Connect(onLeftButtonClick)

I converted index to string and changed indexMin to 1 and max to 2, they were 1 number lower.

1 Like