Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Repeat only executes once, why and how?

Hello!

So, today I was making a game with a shop and I was making a shop button. I was making an effect when you hover over it the text size smoothly goes up to 30, then smoothly goes back down to 25 (the starting size). However, the repeat only executes once and ends. This shouldn't happen, of course, and I can't find the source of it. Also, I tried debugging this myself so if you see any bad practices for debugging please point it out to me because this is the debugged version of my script.

local TweenService = game:GetService("TweenService")
local textButton = script.Parent
local buttonBackground = script.Parent.Parent.Frame
local shopEnabled = false


local pressedPosition = {}
pressedPosition.BackgroundColor3 = Color3.new(0.666667, 0.666667, 0.666667)
pressedPosition.Position = UDim2.new(0, 0, 0.465, 0)

local tweenInfo = TweenInfo.new(
	
	0.5,
	Enum.EasingStyle.Cubic,
	Enum.EasingDirection.InOut,
	0,
	true,
	0
)



local function click()
	local tween = TweenService:Create(textButton, tweenInfo, pressedPosition)
	tween:Play()
	textButton.Sound:Play()
	if shopEnabled == false then
		script.Parent.Parent.Parent.ShopGui.Enabled = true
		shopEnabled = true
	elseif shopEnabled == true then
		script.Parent.Parent.Parent.ShopGui.Enabled = false
		shopEnabled = false
	end
end

local function hover()
	print("getting ready")
	if textButton.TextSize < 30 then
		print("ready")
		repeat textButton.TextSize = textButton.TextSize + 1 print("playing") wait(0.01) until 		textButton.MouseLeave or textButton.TextSize == 30
	end
end

local function hoverReverse()
	print("getting ready")
	if textButton.TextSize > 25 then
		print("ready")
		repeat textButton.TextSize = textButton.TextSize - 1 print("playing") wait(0.01) until textButton.MouseEnter or textButton.TextSize == 25
	end
end
	

textButton.MouseButton1Click:Connect(click)
textButton.MouseEnter:Connect(hover)
textButton.MouseLeave:Connect(hoverReverse)

Location of this script is in a text button inside of a gui with a frame ("background" of button aka 3d effect) and, of course, the gui is inside of starter gui. The click function is an animation for the button when pressed where it clicks down.

Thanks a lot,

Narwhal

6 comments

0

You set up TweenService earlier in the script, why didnt you use that?

0

It doesn't change the actual text size so I can't have that as the base of the second tween. If I could, I am pretty sure it'd be more difficult than this.

0

Not sure what you mean, Tweens can change any numerical value of any instance. Did you try write any code with tweens yet? If so I'd like to see it. Also creating tweens manually gets really really complicated, so I wouldn't do it unless you have a good understanding of interpolation algorithms.

0

Yes I did, but scrapped it. I get it now though. Thing is it still didn't work for me for some reason. If you would like you can show me how I could approach this.

0

Could you edit the post and add your attempt at the end, even if it isnt complete? It would help identify what you are doing wrong.

0

I deleted it though.

2 answers

✓ Accepted answer

Not sure where your Lua experience stands, so ill explain absolutely everything.

TweenService works by creating a tween instance that contains all your movement data. It has 3 properties: the object you want to tween (this can be a 3d object, GUI or just any instance with numerical properties), a TweenInfo instance (this contains the information about your tween), and finally the parameters of the object you want to manipulate. TweenInfo has a number of properties: the duration of the tween (in seconds), the interpolation method (Ex. Linear, Quadratic, Exponential, Back, etc.), the directions it should apply the interpolation (should it interpolate on the start to end, just on the start, etc.) and a couple more that you wont really need to worry about.

With that out of the way lets create a tween. For this I will be tweening the instance Part for one second with the interpolation method set to Circular, interpolating only on the way in, to a new position of 1, 0, 0.

Ex.

local Tween = TweenService:Create(Part, TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(1, 0, 0)})

Note: Enums are just dictionaries for numbers, Enum.EasingStyle.Circular is actually just the number 9 but it makes it easier to remember what 9 actually represents when its written like this.

After the tween is set up you need to actually run it. This step is pretty easy.

Ex.

local Tween = TweenService:Create(Part, TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(1, 0, 0)})

Tween:Play() -- Execute the tween

lastly Tweens can be canceled or paused with :Cancel() and :Pause()

Ex.

local Tween = TweenService:Create(Part, TweenInfo.new(6, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(1, 0, 0)})

Tween:Play()

wait(2)

Tween:Pause() -- Pause the tween

wait(2)

Tween:Play() -- Resume the tween

wait(2)

Tween:Cancel() -- Cancel the tween

Make sure to set the time and interpolation method to whatever you want.

Addendum: "How would I go about when a person puts their mouse outside of the button in the midst of a tween?"

local EnterTween = TweenService:Create(Part, TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(1, 0, 0)})

local ExitTween = TweenService:Create(Part, TweenInfo.new(1, Enum.EasingStyle.Circular, Enum.EasingDirection.In), {position = Vector3.new(0, 0, 0)})

GUI.Button.MouseEnter:Connect(function() -- This is just a different way to set up functions, it works the exact same as your method, I'm just more accustomed to using this. So its not required for this to work.
	ExitTween:Cancel()
	EnterTween:Play()
end)

GUI.Button.MouseLeave:Connect(function()
	EnterTween:Cancel()
	ExitTween:Play()
end)

See if that fixes the issue.

3 comments

0

How would I go about when a person puts their mouse outside of the button in the midst of a tween?

0

Create two variable you will use to store the tweens before ether of the functions, in each function cancel the oposite tween before doing everything. I'll add some example code to my post.

0

I trust you in the fact that this works. Here ya go, acceptance for your answer!

You are using textButton.MouseLeave and textButton.MouseEnter as conditions but these two are events and if they stand alone as an condition lua will automatically check if these are defined or not and as it is defined by default it will instantly stop and exit as you condition was fullfilled

This discussion is preserved as it was. New answers, comments, and votes are closed.