How do I make my detection bar go down?

Hello everyone! I’m in mid of making a stealth game tutorial, here on devforum dot com.
I’m trying to make the detection bar, and I’ve managed to make it go up when they are in detection conditions flawlessly. Now, I have to make it go down once they’re not, and I’m having quite a hard time.
This is the best result I’ve got:


Here’s the script: (I edited out the rest of the script)

local function onRenderStepped(dt: number)
	
	-- detection bar part
	if isDetecting then -- if detecting

		if crouching then

			detectionRate = distance / 2

		else

			detectionRate = distance / 100

		end

		-- add our detection rate to the current detection + clamping it
		detection = math.clamp(detection + detectionRate,0,100)

		if not detectionBarGui.Enabled then

			-- if our gui is not showing up, make it show
			detectionBarGui.Enabled = true

		end

		if detection >= maxDetection then

			-- if we're at at max detection

			detected = true
			-- TODO make our enemies react

		else

			-- resize our detection bar, dividing the detection by the max detection
			detectionBarGuiBar.Size = UDim2.new(1,0,-(detection/maxDetection),0)

		end

	else

		-- we are not being detected

		if detectionBarGuiBar.Size ~= UDim2.new(1,0,0,0) then

			detection = math.clamp(detection - detectionRate,0,100)
			print(detection)
			-- making our bar go down
			detectionBarGuiBar.Size = UDim2.new(1,0,detection/maxDetection,0)

		elseif detectionBarGui.Enabled then

			-- if our gui is showing, make it not show anymore
			detectionBarGui.Enabled = false

		end

	end

end
  • distance is the distance between the player and dummy;
  • crouching is wherever the player is crouching or not;
  • maxDetection is 100

I’d appreciate any help!

2 Likes

Ok so, why not try to make it so it starts from the current size it is and let tween it down say for example:

The detection bar goes half way when you aren’t detected anymore you tween the size from the size of the frame when you weren’t detect anymore to your desired size.

you could also do something like a made shift healthbar. so when you’re detected it increases the value in which the frame increases its size to the value of the numbervalue and when you aren’t detected it starts decreases the value slowly which in turn decreases the size of the frame

My problem is, doing the math to get the desired size.
Additionally, tweening will be super glitchy since it runs on the .RenderStepped event.

Tweening is difficult using the renderstepped event indeed, it would be much easier not using tweenservice and instead a renderstepped event

you could try using TweenSize, it works for renderstepped events

Indeed, I am using it. I have to figure out what to change here

detection = math.clamp(detection - detectionRate,0,100)
print(detection)
-- making our bar go down
detectionBarGuiBar.Size = UDim2.new(1,0,detection/maxDetection,0)

I don’t need any methods of changing the size or whatever, only what calculations for it to properly go down.

if detected then
Bar:TweenSize(UDim2.new(0,0,0,0),“Out”,“Linear”,true)

If you wanted to tween you could use TweenSize it doesn’t require the .TweenService it doesn’t gltich and
it doesn’t affect the speed of the tween.

So you could possible use the TweenSize with the renderstepped Event

Found a solution, it was as simple as putting a - before the math

detectionBarGuiBar.Size = UDim2.new(1,0,-(detection/maxDetection),0)