How to make wires that connect together and work?

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

  1. What do you want to achieve? Keep it simple and clear!
    To make wires that work where there is a wire, the wire connects to a part and a lever through a list or a table that tracks whether a lever is inside of that list and does a remote event. (sry for my bad explanation, im not good at english)
  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub? Searched on devforum but they were mainly forums on continuing on from different scripts

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
If you could provide an outline of what I should do, that would be really helpful :slight_smile:

here is a diagram that can help you:

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

4 Likes
local Wires = {};

local function AddConnection(Connector, targetConnection)
    if (not Wires[Connector]) then
        Wires[Connector] = {targetConnection};
    elseif (not table.find(Wires[Connector], targetConnection)) then
        table.insert(Wires[Connector], targetConnection);
    end
    if (not Wires[targetConnection]) then
        Wires[targetConnection] = {Connector};
    elseif (not table.find(Wires[targetConnection], Connector)) then
        table.insert(Wires[targetConnection], Connector);
    end
end

local function RemoveConnection(Connector, Connection)
    if (Wires[Connector]) then
        local Index = table.find(Wires[Connector], Connection);
        if (Index) then
            table.remove(Wires[Connector], Index);
        end
    end
    if (Wires[Connection]) then
        local Index = table.find(Wires[Connection], Connector);
        if (Index) then
            table.remove(Wires[Connection], Index);
        end
    end
end

local function FindConnections(Connection)
    if (not Wires[Connection]) then return; end -- Connection is not connected to anything!
    
    local Connections = {};
    local Iterate; Iterate = function(Connector)
        if (Wires[Connector]) then
            for _,SubConnection in pairs(Wires[Connector]) do
                if (not table.find(Connections, SubConnection)) then
                    table.insert(Connections, SubConnection);
                    Iterate(SubConnection);
                end
            end
        end
    end ; Iterate(Connection);

    return Connections;
end

Something like this?

Essentially if you want to connect two ‘Connectors’ together (e.g. Model1 and Model2), do:

AddConnection(Model1, Model2);

The Wires table will look like this after:

local Wires = {
    [Model1] = {Model2};
    [Model2] = {Model1};
}

The FindConnections function will try to locate the object you’re referencing from the table, then iteratively loop through every connection and add it to a table. Then all you need to do is search through the returned table and check to see if any of them is a battery.

I’ve not tested it, so there may be some errors. There may be a more efficient way of doing this.

I’ve done it this way for expandability, so you should be able to reference any connection at any point on the circuit and get everything connected to it. It also means you can potentially have multiple things connects to the same thing and it will still work.

Edit: I’ve tested it now, it’s working as intended:

Utilise beams to visualise

3 Likes

To combine this with the visualisation:

local Wires = {};
local Beams = {};

local function CreateWire(Connector, Connection)
	local Exists;
	for _,Beam in pairs(Beams) do
		if (Beam[1] == Connector or Beam[1] == Connection) and (Beam[2] == Connector or Beam[2] == Connection) then
			Exists = true;
			break;
		end
	end
	if (not Exists) then
		local newWire = Instance.new('Beam', Connector);
		newWire.Attachment0 = Connector:FindFirstChildOfClass('Attachment') or Instance.new('Attachment', Connector);
		newWire.Attachment1 = Connection:FindFirstChildOfClass('Attachment') or Instance.new('Attachment', Connection);
		newWire.Width0 = 0.2;
		newWire.Width1 = 0.2;
		newWire.Enabled = true;
		Beams[newWire] = {Connector, Connection};
	end
end

local function RemoveWire(Connector, Connection)
        if (Connector == Connection) then return; end
	for Inst,Beam in pairs(Beams) do
		if (Beam[1] == Connector or Beam[1] == Connection) and (Beam[2] == Connector or Beam[2] == Connection) then
			Inst:Destroy();
			Beams[Inst] = nil;
		end
	end
end

local function AddConnection(Connector, targetConnection)
	if (not Wires[Connector]) then
		Wires[Connector] = {targetConnection};
	elseif (not table.find(Wires[Connector], targetConnection)) then
		table.insert(Wires[Connector], targetConnection);
	end
	if (not Wires[targetConnection]) then
		Wires[targetConnection] = {Connector};
	elseif (not table.find(Wires[targetConnection], Connector)) then
		table.insert(Wires[targetConnection], Connector);
	end
	CreateWire(Connector, targetConnection);
end

local function RemoveConnection(Connector, Connection)
	if (Wires[Connector]) then
		local Index = table.find(Wires[Connector], Connection);
		if (Index) then
			table.remove(Wires[Connector], Index);
		end
	end
	if (Wires[Connection]) then
		local Index = table.find(Wires[Connection], Connector);
		if (Index) then
			table.remove(Wires[Connection], Index);
		end
	end
	RemoveWire(Connector, Connection);
end

local function FindConnections(Connection)
	if (not Wires[Connection]) then return; end -- Connection is not connected to anything!

	local Connections = {};
	local Iterate; Iterate = function(Connector)
		if (Wires[Connector]) then
			for _,SubConnection in pairs(Wires[Connector]) do
				if (not table.find(Connections, SubConnection)) then
					table.insert(Connections, SubConnection);
					Iterate(SubConnection);
				end
			end
		end
	end ; Iterate(Connection);

	return Connections;
end


You can modify the properties of each ‘beam’ in the script to your own preference. See the wiki link I shared in my last post for a full breakdown of those properties.

2 Likes

Thanks! I adjusted the script a bit to fit my game, but not sure where I should put the script? Serverscriptservice? StarterPlayer? Thank you

I’d probably recommend ServerScriptService for most cases. You can send connection requests to the server via RemoteFunctions/RemoteEvents if you want LocalScripts to be able to interact with it. If you did it in StarterPlayer then only that player would see the wires or be able to access their connections. Depends what your aims are for it.

1 Like

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