How can I detect when a GUIS size has changed?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to know how I can detect when a GUIs size has changed.

  2. What is the issue? I don’t know how to.

  3. What solutions have you tried so far? I looked for solutions on YT and dev forum, but I haven’t found any so far.

I need to detect when a GUis size, mainly X/Y’s offset/scale(like a single number). Please help, I need all the advice I can get!

3 Likes

One way of doing it can be by using the .Changed event which fires whenever a property changed in the object:

gui.Changed:Connect(function(ChangedProperty) 
	if ChangedProperty == "Size" then
		-- Everything here will run if the guis' size changed
	end
end)

The function returns a string which describes the name of the property. Therefore, if the string equals to “Size” you know the size property changed.

2 Likes

You can use the GetPropertyChangedSignal() function to listen to any changes made to the specified property in any Instance.

instance:GetPropertyChangedSignal(“PropertyName”):Connect(function()
1 Like

Neither of your code works, I’m trying to make it so when it detects that a change has been made, the new value will be multiplied by a number.

1 Like

Does the code we presented not work, or is it just not suitable?
Cuz you can just save the new size value to a variable or something when you manage to detect it changing.

I made it so when the exp gets more than 1, it times a number to use tweening for a good size UI. I used both your methods and tried to use print() for them, but it would print nothing. When I tested it, it worked for only 1 time but it would stay stuck there. It also wasn’t what I wanted. Heres the code:

	elseif exp.Value > (MaxExp.Value - (MaxExp.Value - exp.Value)) then
		plrExp.Changed:Connect(function(ChangedProperty)
			if ChangedProperty == "Size" then
				local newMth = plrExp.Size.X.Offset
					print(newMth)
					exp.Value *= (newMth + add)
					plrExp:TweenSize(UDim2.new(0, (exp.Value * (mth + add)),1.129, 0), "Out", "Linear", 0.5, false)
			end
		end)

I am not sure what the issue can be. I tested it myself with the same code I wrote here and it worked everytime I changed the size. Maybe there could be some problem with your code, but I don’t see anything problematic.

if GetPropertyChangedSignal Dosent work try this code replace the NewValue with the size of the gui and oldvalue with also the size of the UI

local OldValue = 100
while wait() do
	local NewValue = 2
	if NewValue ~= OldValue then
		print("Changed")
	end
	OldValue = NewValue
end

Heres my entire script then:

local lvlFrame = script.Parent:WaitForChild("LevelFrame")
local maxExp = lvlFrame:WaitForChild("MaxExp")
local plrExp = maxExp:WaitForChild("PlrExp")
local futureLvl = lvlFrame:WaitForChild("FutureLevel")
local add = lvlFrame:WaitForChild("Add").Value
local currentLvl = lvlFrame:WaitForChild("CurrentLevel")
local tween = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local exp = plr.Exp
local MaxExp = plr.MaxExp
local lvl = plr.Level
local futLvl = plr.FutureLevel

function newLevelGui()
	plrExp:TweenSize(UDim2.new(0, 0,1.129, 0), "Out", "Linear", 0.5, false)
	currentLvl.Text = lvl.Value
	futureLvl.Text = futLvl.Value
end


function updatePlrLvl()
	lvl.Value += 1
	futLvl.Value += 1
	exp = 0
	MaxExp.Value += 50
end

local mth = plrExp.Size.X.Offset

plr.Exp.Changed:Connect(function()
	if exp.Value == (MaxExp.Value - (MaxExp.Value - exp.Value)) then
		plrExp.Size = UDim2.new(0, (mth + add), 1.129, 0)
	elseif exp.Value > (MaxExp.Value - (MaxExp.Value - exp.Value)) then
		plrExp.Changed:Connect(function(ChangedProperty)
			if ChangedProperty == "Size" then
				local newMth = plrExp.Size.X.Offset
					print(newMth)
					exp.Value *= (newMth + add)
					plrExp:TweenSize(UDim2.new(0, (exp.Value * (mth + add)),1.129, 0), "Out", "Linear", 0.5, false)
			end
		end)
	elseif exp.Value == MaxExp.Value then
		newLevelGui()
		updatePlrLvl()
	end
end)

the problem is that when it changes, the newMth variable doesn’t work. i think

local Gui = script:FindFirstAncestorOfClass("ScreenGui")
local Frame = Gui:FindFirstChildOfClass("Frame")

local Resized = Frame:GetPropertyChangedSignal("AbsoluteSize")

Resized:Connect(function()
	print("The frame's size has changed!")
end)

This works as I’ve just tested it.

What if i wanted to find when a specific property of the size, like the offset/scale of X, what would I put?

Oi, back at it again with this case.
You can make a variable to store the size value in. You can use it to check for changes and save it for later:
(I have tested this and it works)

local gui = script.Parent -- This will refer to your gui object
local lastOffset_X = gui.Size.X.Offset -- Set the value to the current gui size right away to act as the base value

gui.Changed:Connect(function(property)
	if property == "Size" then
		if gui.Size.X.Offset ~= lastOffset_X then -- if our new size X offset is not equal to the saved one, overwrite it with the new one
			print("Last Size: " .. lastOffset_X .. "  New Size: " .. gui.Size.X.Offset)
			lastOffset_X = gui.Size.X.Offset -- overwriting
		end
	end
end)

Hope this time it helps.