Help with using Tables

Been wondering how can I use tables, since this will be so useful for me and much more simpler way to handle and organize my codes. Pardon me, I’m still new to using tables

local button = {
	[1] = script.Parent:WaitForChild("1Pic"):WaitForChild("Image"),
	[2] = script.Parent:WaitForChild("2Pic"):WaitForChild("Image"),
	[3] = script.Parent:WaitForChild("3Pic"):WaitForChild("Image")
} 

-- // same goes for this part too, I don't know how to use tables to reference
-- // all of the buttons for the tween: ...(button[1], ...)
local TweenOut = TweenService:Create(button[1], IconTween, GoalOut)
local TweenIn = TweenService:Create(button[1], IconTween, GoalIn)

-- // I want to use the Table I made to reference all of the buttons in this
-- // code so whenever my Mouse hovers at any at the Table, it'll just play the Tween
button[1].MouseEnter:Connect(function() 
	TweenIn:Play()
end)

button[1].MouseLeave:Connect(function()
	TweenOut:Play()
end)
1 Like

Using tables for that isnt really optimized, you should just store them all in a frame and just assign a variable to the frame. then you can get the buttons from it.

Currently this is what my Explorer should look like

image

do i, v in pairs(frame:GetChildren()) and assign them the tweens.

Make a pairs loop from the scrolling frame and check if its a frame. Then connect the tween functions if it is a frame.

Basically:


for _, v in pairs(script.Parent:GetChildren()) do
  if v:isA("Frame") then
v.MouseEnter:Connect(function()
tweenIn(v)
end)
v.MouseLeave:Connect(function()
tweenOut(v)
end)
end
end

let me try this one and see if it will work, I’ll combine all of the image in 1 Frame

Dont change the hierarchy. Let it stay the same and it will work.

how about for these part?

local TweenOut = TweenService:Create(button[1], IconTween, GoalOut)
local TweenIn = TweenService:Create(button[1], IconTween, GoalIn)

They should become functions with the frame as the first parameter. Inside the function, you should tween the frame in the parameter.
remember to play it btw

ohh so something like this?

local TweenOut = TweenService:Create(IconTween, GoalOut)
local TweenIn = TweenService:Create(IconTween, GoalIn)

for _, v in pairs(script.Parent.Parent:GetChildren()) do
	if v:isA("Frame") then
		v.MouseEnter:Connect(function() -- Should I put the TweenInfo here?
			TweenIn(v) -- and what is this purpose?
			-- should I put the tween:play here inside too?
		end)
		v.MouseLeave:Connect(function()
			TweenOut(v)
		end)
	end
end

sorry, I have so many questions :sob:

local function TweenIn(function(frame)
TweenService:Create(frame,TweenInfo.new(.1),GoalIn):Play(
end)
local function TweenOut(function(frame)
TweenService:Create(frame,TweenInfo.new(.1),GoalOut):Play(
end)

OHHH my god- I didn’t realize that. I’m getting the hang of it now.

So basically:

for _, v in pairs(script.Parent.Parent:GetChildren()) do
	if v:isA("Frame") then -- // This Detects if the Chilren are a Frame, correct?
		v.MouseEnter:Connect(function()
			TweenIn(v) -- this is the function, and the first parameter is sent
					-- to the function TweenIn.
		end)
...

yeah got it? do it work? do it look good?

Hold on, let me do the script real quick and I’ll show the results

local function TweenIn(function(frame)
	TweenService:Create(frame, IconTween,GoalIn):Play()
end)

local function TweenOut(function(frame)
			TweenService:Create(frame, IconTween,GoalOut):Play()
end)

for _, v in pairs(script.Parent.Parent:GetChildren()) do
	if v:isA("Frame") then
		v.MouseEnter:Connect(function()
			TweenIn(v)
		end)
		v.MouseLeave:Connect(function()
			TweenOut(v)
		end)
	end
end

some errors in the coding
image

This broke me sorry :sob: Tables are confusing me so bad @keremMCT

I’m not sure if you know this but, assigning a table key to an instance doesn’t actually create a copy of all the data. It only provides a reference. Using tables is fine.

I know but its not necessary in this situation.

The OP’s method was perfectly fine, what if the OP didn’t want some buttons inside the frame?

local function TweenIn(frame)
	TweenService:Create(frame, IconTween,GoalIn):Play()
end

local function TweenOut(frame)
	TweenService:Create(frame, IconTween,GoalOut):Play()
end

for _, v in pairs(script.Parent.Parent:GetChildren()) do
	if v:isA("Frame") then
		v.MouseEnter:Connect(function()
			TweenIn(v)
		end)
		v.MouseLeave:Connect(function()
			TweenOut(v)
		end)
	end
end

Functions were not structured correctly.

i don’t see the use in having two seperate tween functions especially since you aren’t waiting for the tweens

local TS = game:GetService("TweenService")
local tInfo = TweenInfo.new()
local Games = script.Parent:GetChildren() -- Assuming these buttons will teleport you to games 

for _, GameFrame in Games do -- ipairs() would work too
	if not GameFrame:IsA("Frame") then continue end
	local GameButton = GameFrame.Button -- I'd suggest renaming 'Image' to 'Button'
	GameButton.MouseEnter:Connect(function()
		TS:Create(GameButton, tInfo, InGoal):Play()
	end)

	GameButton.MouseLeave:Connect(function()
		TS:Create(GameButton, tInfo, OutGoal):Play()
	end)
end