How would I make mousebutton1down / up more consistant?

ShopButton.MouseButton1Down:Connect(function()

ShopButton:TweenPosition((ShopButtonPosition), "In", "Quad", .15, true)

ShopButton:TweenSize(UDim2.new(0.522, 0,0.277, 0),"In","Quad",.15,false)

UiClick:Play()

end)

ShopButton.MouseButton1Up:Connect(function()

ShopButton:TweenSize((ShopButtonSize),"In","Quad",.15,false)

end)

When I hold the button down and release with a little delay it works fine, but when I change everything to one code with mousebutton click it wont work. My question is how would I make this script more consistent so when I click the button it plays the small → large tween

Example: https://gyazo.com/f1ee0a4b928dd5c25eee87eafec9be99

You can use the event MouseEnter and MouseLeave to detect when to set buttons size back to default. When the MouseLeave event fires you can set it to the default size. More on this here:


TextButton | Roblox Creator Documentation

This is my whole script, I am already using mouse enter and leave

-- Buttons
local ShopButton = script.Parent.Buttons:WaitForChild("Shop")
local SettingsButton = script.Parent.Buttons:WaitForChild("Settings")

-- Button Positions & Size
local ShopButtonPosition = ShopButton.Position
local ShopButtonSize = ShopButton.Size
local SettingsButtonPosition = SettingsButton.Position
local SettingsButtonSize = SettingsButton.Size

-- Frames
local ShopFrame = script.Parent:WaitForChild("ShopFrame")

-- Sounds
local UiHover = workspace.Sounds:WaitForChild("UIHover")
local UiClick = workspace.Sounds:WaitForChild("UIClick")

-- MouseEnter & Leave Tweens
ShopButton.MouseEnter:Connect(function()
	ShopButton:TweenPosition(UDim2.new(.318,0,.319,0), "In", "Quad", .15, true)
	UiHover:Play()
end)

ShopButton.MouseLeave:Connect(function()
	ShopButton:TweenPosition((ShopButtonPosition), "In", "Quad", .15, true)
end)

SettingsButton.MouseEnter:Connect(function()
	SettingsButton:TweenPosition(UDim2.new(.948,0,.319,0), "In", "Quad", .15, true)
	UiHover:Play()
end)

SettingsButton.MouseLeave:Connect(function()
	SettingsButton:TweenPosition((SettingsButtonPosition), "In", "Quad", .15, true)
end)

-- MouseClick Tweens
ShopButton.MouseButton1Down:Connect(function()
	ShopButton:TweenPosition((ShopButtonPosition), "In", "Quad", .15, true)
	ShopButton:TweenSize(UDim2.new(0.522, 0,0.277, 0),"In","Quad",.15,false)
	UiClick:Play()
end)

ShopButton.MouseButton1Up:Connect(function()
	ShopButton:TweenSize((ShopButtonSize),"In","Quad",.15,false)
end)

So, what type of animation are you trying to achieve with these buttons? The size goes big and then small?

Small then big, when the mouse goes down it gets small, when released its back to normal. Like the adopt me UI

If that’s what you’re trying to accomplish I would recommend you handle the entire animation on the MouseButton1Down event.

An example of this solution would be:

--function above
	ShopButton:TweenPosition((ShopButtonPosition), "In", "Quad", .15, true)
	ShopButton:TweenSize(UDim2.new(0.522, 0,0.277, 0),"In","Quad",.15,false)
    UiClick:Play()
    task.delay(.15,function()
      ShopButton:TweenSize((ShopButtonSize),"In","Quad",.15,false)
    end)

Hi,

You should use TweenService to accomplish this goal, TweenService is a lot better for this goal as it gives you more options rather than Positioning and Sizing, you can even reverse the Tween to create a form of Click,:

ts = game.TweenService
e = script.Parent

info = TweenInfo.new( -- TweenInfo
	.15,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	true
)
e.MouseButton1Down:Connect(function()
	ts:Create(e, info, {Size = UDim2.new(.031, 0,.119, 0)}):Play() -- Random Size
end)

It doesn’t Require MouseButton1Up for this goal.

You Should also be using UDim2.fromScale() if you are only using the scale parameters, you can also shorten your numbers a bit, like for example, instead of 0.8, you can just do .8 and Lua will still understand it, Here is an Example of UDim2s

UDim2.new(0.522, 0, 0.277, 0) -- if you're using both scale and offset
UDim2.fromScale(.522, .277) -- works with Scale
UDim2.fromOffset(0, 0) -- works with offset

task.delay() is useless, you can easily reverse the tween with TweenService

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.