I’m trying to make a button move when hovered over, and this is my code so far: (I got a lot of help from StrongBigeMan9)
local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script
local function onHover(item)
local currentpos = item.Position
--[[item:TweenPosition(
UDim2.new(200,0,200,0),-- end size
"Out", -- easing direction
"Quad", -- easing style
1.5, --time in seconds
false --override?
--tween2
)]]--
end
for _, item in ipairs(GuiLayer) do
if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
onHover(item)
end)
end
end
Since the TextButtons are in different positions, tweening them to the same position obviously wouldn’t work. So, how would I make it (the button) so that it adds on stuff (moves forward) on the X axis?
I want it to be like when you are adding Vector3 to a part:
You can reference its current position when adjusting the parameters of the TweenPosition and increment it a certain amount:
local function onHover(item)
local currentpos = item.Position
item:TweenPosition(
UDim2.new(currentpos.Scale.X + number, 0, currentpos.Scale.Y + number, 0)
-- This will take its current X & Y Scale for Position and add it by number
)
end
I’m trying to make it so the button moves back when your mouse leaves, but it doesn’t work very well, could you help me out again? Thanks!
Code:
local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script
local function onHover(item)
local currentpos = item.Position
item:TweenPosition(
UDim2.new(currentpos.X.Scale + 0.03, 0, currentpos.Y.Scale + 0, 0)
)
end
local function onExit(item)
local currentpos = item.Position
item:TweenPosition(
UDim2.new(currentpos.X.Scale + -0.03, 0, currentpos.Y.Scale + 0, 0)
)
end
for _, item in ipairs(GuiLayer) do
if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
onHover(item)
end)
item.MouseLeave:Connect(function()
onExit(item)
end)
end
end
Alright, if you’re back (I gotta go soon) here’s the code that isn’t working:
local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script
local function onHover(item)
local currentpos = item.Position
item:TweenPosition(
UDim2.new(currentpos.X.Scale + 0.03, 0, currentpos.Y.Scale + 0, 0)
"Out", -- easing direction
"Quad", -- easing style
0.1, --time in seconds
false --override?
)
end
local function onExit(item)
local currentpos = item.Position
item:TweenPosition(
UDim2.new(currentpos.X.Scale + -0.03, 0, currentpos.Y.Scale + 0, 0)
"Out", -- easing direction
"Quad", -- easing style
0.1, --time in seconds
false --override?
)
end
for _, item in ipairs(GuiLayer) do
if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
onHover(item)
end)
item.MouseLeave:Connect(function()
onExit(item)
end)
end
end
Error: layers.RipPBB_TUD.PlayerGui.LoadingScreen.LoadingScreen4.TweenScript:18: attempt to call a UDim2 value - Client - TweenScript:18
I also added some print functions, and it looks like both are running, but there’s the error.
I have no idea what’s wrong, so I can’t really fix it. I also don’t get what was wrong with my original code without the other parameters of the TweenPosition method.
Edit: I removed the parameters which were causing the error, and it seems like one is running right after the other making it weird. Would I make it override or something? No idea how to fix this too.
I think it’s cause you forgot to put a comma , after making the UDim2s in the functions, also, Iwould recommed making the override true so the tween can be changed mid tween
local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script
local function onHover(item)
local currentpos = item.Position
print("run")
item:TweenPosition(
UDim2.new(currentpos.X.Scale + 0.02, 0, currentpos.Y.Scale + 0, 0),
"Out", -- easing direction
"Quad", -- easing style
0.2, --time in seconds
true --override?
)
end
local function onExit(item)
local currentpos = item.Position
print("run")
item:TweenPosition(
UDim2.new(currentpos.X.Scale + -0.03, 0, currentpos.Y.Scale + 0, 0),
"Out", -- easing direction
"Quad", -- easing style
0.1, --time in seconds
true --override?
)
end
for _, item in ipairs(GuiLayer) do
if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
onHover(item)
end)
item.MouseLeave:Connect(function()
onExit(item)
end)
end
end
Also wait, are you able to show a reproduction of the issue you’re facing? Maybe try try setting the overrides to false since the time is extremely small?