Progress bar stuck on an old value

hey, so for my game i made a progress bar that indicates the number of buttons in the game at the moment
(status changes to the number of buttons pressed out of the buttons on the map)

it works for the first map, but the number of buttons on a map doesn’t change, so for example i could have 7 buttons on the new map but it’ll still say 3 buttons because it was on the old map, here’s an example of that:

the code is in a localScript inside the progress bar itself (red one)

local progressBar = script.Parent
local buttonsPressed = game.Workspace.ButtonsPressed
local status = script.Parent.Parent.Status

local buttons = game.Workspace.Buttons:GetChildren()

buttonsPressed.Changed:Connect(function(Changed)
	if Changed then
		progressBar:TweenSize(UDim2.new(buttonsPressed.Value/#buttons, 0,1,0))
	end
end)


while wait() do
	status.TextLabel.Text = buttonsPressed.Value.."/"..#buttons
end

is there a way to do a constant check on how many buttons are in a map through a local script?

thank you in advance, any help/advice is appreciated

The simplest edit would be to simply add the line below into the while loop; however, that obviously wouldn’t be the most efficient approach.

buttons = game.Workspace.Buttons:GetChildren()

A more efficient method could be to change the text within the Changed event function (Instead of the loop) and to update the buttons variable each time you switch maps, possibly with a remove event.

i usually don’t use remote events, mostly because i don’t have much experience with them, so how can i go about doing so?

Ill provide a very vague run down as there are a ton of posts/threads regarding the matter, and the wiki is also a solid source of information.

In short remote events allow communication between scripts.

In your particular case, I would assume that a new map is generated via a server sided script. From that script you can fire an “event” to all the clients and receive it within the script you provided. All you would need is to connect a function to the event, very much similar as to how you created the Changed function. Within the function all you would need to to update the buttons variable’s value

The section I snipped is what I assume will be most helpful in this situation.

In essence you run remoteEventVariable:FireAllClients() to send the signal and can create a function as such (below) to receive the signal.

remoteEventVariable.OnClientEvent:Connect(function()
   button = ...
end)

i’m not quite sure i understand all of this, i get a vague idea but i’m simply not sure on how i’d actually apply it or what i’d have to edit in my scripts to make it work properly