Variable not updating in function?

I want a function that cleans up old functions to prevent confliction.

  1. disconnect old functions
  2. Connect new functions
    2a. Have player input happen
  3. When player is done, disconnect functions by re-using the refresh_Edit_Data function

The problem is that connected_Edit1 in this function:

local function refresh_Edit_Data()
				if connected_Edit1 then
					connected_Edit1:Disconnect()
				end
			end

Does not update when
connected_Edit1 is assigned a connected function.
Assigning connected_Edit1 below:

connected_Edit1 = input_folder.EditScale.Value.MouseButton1Click:Connect(function()
						
						print(connected_Edit1)
						
						if hit.Parent:WaitForChild("ObjInfo") then
							if hit.Parent.ObjInfo:WaitForChild("IsProcedural").Value == true then

								game.ReplicatedStorage.BuildingServiceV2.events:WaitForChild("scaleObjectToggle"):FireServer(hit.Parent)
								
							end	
						end
					end)

More Info:

Full code below:
|
|
|

	local connected_Edit1
			
			-- This function 
			-- 1. Disconnects old functions to prevent conflicts
			local function refresh_Edit_Data()
				if connected_Edit1 then
					connected_Edit1:Disconnect()
				end
			end
			
			local hit = mouse.Target
			if hit then
				
				-- checking if valid object
				-- NOTE: edit menu buttons below here
				if hit.Parent.Parent.Name == "Objects" then

					refresh_Edit_Data()
					
					nonInputs_folder:WaitForChild("EditMenu").Value.Enabled = true

					connected_Edit1 = input_folder.EditScale.Value.MouseButton1Click:Connect(function()
						
						print(connected_Edit1)
						
						if hit.Parent:WaitForChild("ObjInfo") then
							if hit.Parent.ObjInfo:WaitForChild("IsProcedural").Value == true then

								game.ReplicatedStorage.BuildingServiceV2.events:WaitForChild("scaleObjectToggle"):FireServer(hit.Parent)
								
							end	
						end
					end)
				else
					
					nonInputs_folder:WaitForChild("EditMenu").Value.Enabled = false
					
					refresh_Edit_Data()
				end
			end

You’re connecting a function after your refresh_Edit_Data runs, which is why it prints nil. I’m not sure how you want it to be structured.

1 Like

But then when I run it a second time (which it will) it still is nil

Move the connected_Edit1 outside of the function. It’s resetting every time because it’s scope is in the function.

1 Like

Edit:

  1. disconnect old functions
  2. Connect new functions
    2a. Have player input happen
  3. When player is done, disconnect functions by re-using the refresh_Edit_Data function

Dang just noticed that. This is probably to solution

Thanks, would not have seen that for a while. Always nice to have a fresh set of eyes. Have a nice Christmas!

1 Like

No problem, and sorry for the wait, I was just eating lunch.

You too!

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