How to detect when a part is small enough

there’s this part that i have, which constantly shrinks, using this script:

while true do
	wait(1)
	script.Parent.Size -= Vector3.new(0,0.02,0)
end

i’ve been using that shrinking part as a bar, which was supossed to kill the player if it got too low. but so far i’ve been having trouble detecting when the bar was too small.

local part = script.Parent
if part.Size.Y <  3.700 then
	print("o2 works")
end

i’m a huge beginner, any help appreciated.

1 Like

Seems like it should work but the check needs to be called every time the bar shrinks.

make it do a check every time the part shrinks but in a loop.

i’ve tried

while true do
	local part = script.Parent
	if part.Size.Y <  3.700 then
		print("o2 works")
	end
end

but still doesn’t print anything.

you can use vector3.Magnetude to compare size of a part

To make it trigger after it shrink to a certain size use GetPropertyChangedSignal()

which will look something like this

part:GetPropertyChangedSignal("Size"):Connect(function()
	if part.Size.Magnitude < TargetSize.Magnitude then
		--script here
	end
end)
Full Script
local part = script.Parent

local ShrinkAmount = Vector3.new(.02,.02,.02)
local TargetSize = Vector3.new(3.7,3.7,3.7)

part:GetPropertyChangedSignal("Size"):Connect(function()
	if part.Size.Magnitude < TargetSize.Magnitude then
		--script here
	end
end)

while true do
	part.Size -= ShrinkAmount
	wait()
end

alternatively if you want to shrink a part smoothly I recommend using TweenService() so you don’t need to run a loop and it is much easier

Useful link:
TweenService
Vector3
Magnitude

1 Like

this worked perfectly, thank you so much!

1 Like

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