:Clone() breaks my code?

I seem to be encountering an issue with the :Clone() when i clone one of my components in my circuit game it just breaks.

An example of this, when i clone of the Logic Gates you should be able to plug a plug into the output of the gate (the socket) but it doesnt plug in at all, and the touched event doesnt even work?

The server script below that is supposed to manage the power of the socket and plugs just stops. It doesnt power and does nothing? what happened i am confused.

Powering and plugging script that breaks when the certain part is cloned:


local socketsFolder = script.Parent
local sockets = collectionService:GetTagged("Socket")
local cooldown = 1

local function createWeld(part1, part2)
	local newweld = Instance.new("WeldConstraint")
	newweld.Parent = part1
	newweld.Part0 = part1
	newweld.Part1 = part2
end

local function destroyWelds(part)
	if part:FindFirstChildOfClass("WeldConstraint") then
		part.WeldConstraint:Destroy()
	end
end

local function updatePower(plugPower, socketPower)
	if socketPower.Value == true then
		plugPower.Value = true
	else
		plugPower.Value = false
	end
end

for i, socket in pairs(sockets) do -- goes throught all of the sockets that have the "Socket" tag.

	local hitBox = socket:FindFirstChild("HitBox")
	local proximityPrompt = hitBox:FindFirstChild("ProximityPrompt")
	local socketPower = socket.Configurations:FindFirstChild("Powered")
	local powerSwitch = socket.Configurations:FindFirstChild("SwitchPower") -- determines which way the power will go, plug to socket if set to true

	local debounce = false

	local plug -- nil value for plug

	hitBox.Touched:Connect(function (part)


		local connected = part:FindFirstChild("Connected") -- value to check whether the plug is connected

		if collectionService:HasTag(part, "Plug") and not debounce then

			if connected and connected:IsA("BoolValue") and connected.Value then
				return
			end

			debounce = true
			
			print("touched")
			
			plug = part -- set the nil plug value to the actual plug object
			print(tostring(plug) .. " has plugged into " .. tostring(socket))

			local plugPower = plug:FindFirstChild("Powered")
			local dragDetector = plug:FindFirstChild("DragDetector")

			plug.CFrame = hitBox.CFrame -- move the plug to the destination

			createWeld(plug, hitBox)
			updatePower(plugPower, socketPower)

			dragDetector.Enabled = false
			proximityPrompt.Enabled = true
			connected.Value = true

		end
	end)

	socketPower.Changed:Connect(function ()

		if plug then

			local plugPower = plug:FindFirstChild("Powered")

			updatePower(plugPower, socketPower)

			print("socket power" , socketPower.Value)	
			print("plug power" , plugPower.Value)	
		end
	end)
	proximityPrompt.Triggered:Connect(function ()

		local plugPower = plug:FindFirstChild("Powered")
		local dragDetector = plug:FindFirstChild("DragDetector")
		local connected = plug:FindFirstChild("Connected")

		connected.Value = false

		if plugPower and plugPower:IsA("BoolValue") then
			plugPower.Value = false
		end

		dragDetector.Enabled = true
		proximityPrompt.Enabled = false
		
		destroyWelds(plug)

		plug = nil

		wait(1)

		debounce = false

	end)
end

Heres the script where it clones the model or part:

local GameWorkspace = game:GetService("Workspace")

sendToServerEvent.OnServerEvent:Connect(function (player, object)
	local clonedObject = object:Clone()
	
	if not object.Archivable then
		print("The object is not archivable and cannot be cloned.")
		return
	end
	
	print("Cloned object:", clonedObject:GetFullName())

	if clonedObject:IsA("BasePart") then
		local characterPosition = player.Character.HumanoidRootPart.Position
		local lookVector = player.Character.HumanoidRootPart.CFrame.LookVector
		local distanceInFront = 5 -- Set this to whatever distance you need

		clonedObject.Position = characterPosition + (lookVector * distanceInFront)
		print("BasePart position set to:", clonedObject.Position)
	elseif clonedObject:IsA("Model") then
		local characterPosition = player.Character.HumanoidRootPart.Position
		local lookVector = player.Character.HumanoidRootPart.CFrame.LookVector
		local distanceInFront = 5 -- Set this to whatever distance you need

		clonedObject:PivotTo(CFrame.new(characterPosition + (lookVector * distanceInFront)))
		print("Model pivot set to:", clonedObject:GetPivot())
	end
	
	clonedObject.Parent = GameWorkspace
end)

IMPORTANT NOTE: when i put the components back into workspace, it works fine. So something seems to be going wrong with the cloning, but nothing is lost or anything so i am unsure of the problem.

2 Likes

Do you have archivable enabled on it?

yes, i have checked in the explorer tab. but i can make it so it will make the object archivable just incase in the script

i have checked and there is no sign of anything not archivable

i have fixed the problem, going to close this forum.

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