How could I make a timed bar?

Sorry for the vague title I don’t really know what to call it, I just need to make a bar so if you time it correctly then something happens and if you don’t something else happens, I DON’T want this to all be done for me I just need a idea of how I could do it.

https://gyazo.com/1716eadaf66fa3db8fb6bb64e7d980d2

Heres a placeholder frame I made, I want the little greenbar to move across the bar and when you click it stops, if you click on the black part then its good, the white part bad.

I was thinking maybe tweening the green bar across the frame and having a click stop the tween and then checking what position its in to see if its ontop of the black part? I don’t know if thats the most optimal way of doing it tho.

3 Likes

There are two methods I though of:


Tweening – The Cleaner Way

You could tween the green part across the whole frame, and stop the tween when something is clicked. You could then check its (green part) position and see if it is in the Black Part. You should prefer this, as this is the cleaner way.


For Loop – The Worse Way

You could also do for loop which would go from 0 (start of the white Frame) to its end. You could then somehow break the for loop when something is clicked.

I would though prefer the Tweening way, as it is cleaner and better.

1 Like

The way you described sounds great! If you don’t know about it, you should probably use TweenService, since it seems like it covers everything you want to do.

1 Like

Like the others said, you could use Tweening to move the green bar right to left but as well as that you could use UserInputService to detect when you have clicked.

Along with that, I have created a picture where it shows the start position of the black bar and the end position of the black bar:

As you can see, this is an example, let’s say 60 is the start position of the X axis of the black bar and 90 is the end position of the X axis of the black bar, you will have to make an if statement to see whether the player has clicked, if they clicked then you can probably add this:


local StartPos = UDim2.new(0, 60, 0, 0) -- this is the start position for example

local EndPos = UDim2.new(0, 90, 0, 0) --this is the end position for example

if UserInputService.InputBegan then -- bla bla Your own if statement when the player has clicked with the mouse

if GreenBar.Position >= StartPos or <= EndPos then

--- do whatever when the player has successfully clicked the greenbar within the black bar

     end
end

This is just an example so it’s not accurate.

2 Likes

So it seems most of the system is done but I’m getting a error I can’t figure out

game.ReplicatedStorage.FightingEvents.KnockdownGui.OnClientEvent:Connect(function(player, Difficulty)
	
	--TweenInformation
	local TimerBar = Players.LocalPlayer.PlayerGui.FightingHud.Knockdown.Ticker
	local SpotToHitFrame = Players.LocalPlayer.PlayerGui.FightingHud.Knockdown.SpotToHit
	local TweenTime = nil
	local StartHit = UDim2(0.426, 0, 0, 0)
	local EndHit = UDim2(0.526, 0 , 0, 0)
	Knockdown = true
	
	if Difficulty == 1 then
		TweenTime = 1
	end
	
	if Difficulty == 2 then
		TweenTime = 2
	end
	
	if Difficulty == 3 then
		TweenTime = 3
	end

	local KnockTimerGoals = {}
	KnockTimerGoals.Position = UDim2.new(1, 0 , 0, 0)
	
	wait(1)
	
	local tweenInfo = TweenInfo.new(
		0.5, -- Time
		Enum.EasingStyle.Linear, -- EasingStyle
		Enum.EasingDirection.Out, -- EasingDirection
		-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
		true, -- Reverses (tween will reverse once reaching it's goal)
		0 -- DelayTime
	)
	
		 
	local KnockTimerTween = TweenService:Create(TimerBar, tweenInfo, KnockTimerGoals)
	
		 
	KnockTimerTween:Play()
	
	UserInputService.InputBegan:Connect(function(input, event)
		if input.KeyCode == Enum.KeyCode.Space and Knockdown == true and event == false then
			KnockTimerTween:Pause()
			if TimerBar.Position >= StartHit or TimerBar.Positon <= EndHit then
				print("YouTimedItRight")
			end
		end	
	end)
end)

I’m getting “Can’t Compare 2 UDIM Values” or something long those lines, everything else is working fine it’s just that. I looked around and coulnd’t really see anything that applies to my situation.

Something like (pseudo):

local TweenService = game:GetService("TweenService")
lcoal TweenTime = 3
local totalDivisions = 10
local bar = GuiObjectToTween
local buffer = UDim2.fromScale(bar.Size.X.Scale/ totalDivisions, 0) 

repeat
    local tween = TweenService:Create(bar, TweenInfo.new(TweenTime, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, AddedTime), {Size = bar.Size + buffer}) 
    tween:Play()
    tween.Completed:Wait()
until bar.Size.X.Scale == 3 -- assuming all offsets were converted to scale and that you need it to reach a max size of 3 on its X Scale property
-- change the TweenInfo to your needs

You cant compare UDims since theyre basically structs and not of fundamental type. Try TimerBar.Position.X.Scale >= StartHit.X.Scale etc.

1 Like