Creating a connection missing a model consistently

Hi,

I’ve got a couple of models/parts in a folder in the workspace and I am forming a connection in the code below, for whenever a clickDetector inside of that object gets pressed. All of the code works, but it leaves 1 out. No errors, when I add more, it leaves the last one out again. Any ideas?

With the one model/part that doesn’t work, I just want the ‘toggle 2’ to fire. (It fires from everything, besides the last one)

for i,v in pairs(signals:GetChildren()) do

    if toggle then
      print(v)
      print(toggleConn)
      toggleConn =  v.Toggle.ClickDetector.MouseClick:Connect(function(player)
        print("toggle 2")
        print(v)
        signal_util.toggle_Frequency(player, v)
        if toggleConn ~= nil then toggleConn:Disconnect() end
      end)
    
    end

end

Any help would be greatly appreciated! (I did check, it runs same amount of times as there is a model)

photo of the explorer:
image

image

I don’t understand what you’re trying to do but you set the MouseClick connection to one variable, which leaves the previous in void. Consider turning this into a table use table.insert with the connection?

toggleConn is a variable, and it works for all other 7 models, but ceases at the last. Making the connection such as in a .Touched Event, cant remember where I saw it.

The table breaks it, as I am using toggleConn to activate a function when the clickDetector is fired, therefore a table would break it. (as far as I know. not sure of another way to do it with a table)

EDIT: I looked into the table, it works. (I just forgot to use the [1] to make the connection.
Ex. connections[1] = myEvent:Connect(myFunction)

1 Like

What do you mean by last one?


1 Like

I have a folder filled with 7 models. 1 emitter and rest receivers. The code above works for everything, but it fails on the last one in the list.

Can you send the explorer please?

image

image

Are there click detectors in every single one, also are they all named the same?

Would you be able to send the entire script? Also, what does toggle mean?

Toggle is just a button on the models with the clickDetector. You press it and it changes the frequency.

-- // Services
local serverScriptService = game:GetService("ServerScriptService")

-- // Modules
local signal_util = require(script.Signal_Util)

-- // Folders / variables
local signals = workspace.Signals

-- // Events
game.Players.PlayerAdded:Connect(function(player)
	local freqChange 
	local toggleConn = {}
	local emitterBtnConn
	local recievedSignalConn
	
	-- start with the recievers
	
	for i,v in pairs(signals:GetChildren()) do

		if v:IsA("BasePart") then
			local toggle = v:FindFirstChild("Toggle")
			local config = v.Configuration
	
			if toggle then
				print(v)
				print(toggleConn)
				toggleConn = v.Toggle.ClickDetector.MouseClick:Connect(function(player)
					print("toggle 2")
					print(v)
					signal_util.toggle_Frequency(player, v)
					if toggleConn ~= nil then toggleConn = {} end
				end)
				
				freqChange = config.Frequency.Changed:Connect(function(value)
					signal_util.setFrequency(v, value)
					if freqChange ~= nil then freqChange:Disconnect() end
				end)
			end
			
		end

	end
	
end)

The module:

local frequencies = {
	"F#1",
	"F#2",
	"F#3",
	"F#4",
	"F#5",
}

local signal_Util = {}

function signal_Util.setFrequency(object, value)
	object.SurfaceGui.TextLabel.Text = value

end

function signal_Util.toggle_Frequency(player, object)
	for i,v in frequencies do
		if v == object.Configuration.Frequency.Value then
			local index = i + 1
			if index > 5 then
				index = 1
			end

			object.Configuration.Frequency.Value = frequencies[index]
			object.SurfaceGui.TextLabel.Text = frequencies[index]
			
			print("updated frequency")
			break
		end
	end
end

return signal_Util

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