Clone() function won't clone twice

I’m making a machine gun which when you sit down, clones a “control” script into your character. However, if you get up and sit back down on the machine gun, it doesn’t clone again.

script.Parent.Changed:Connect(function()
	print("hiii")
		local a = script.Parent.Occupant
		local b = game.Players:FindFirstChild(a.Parent.Name)
	if b~= nil then
		print("NOOOO")
		if a.Parent:findFirstChild("control") == nil then
			local g=script:WaitForChild("control"):Clone()
			print("IT WORKED")
			g.Value.Value=script.Parent.Parent.Parent
			g.Parent=b.Character
			g.Disabled=false
			occupier = b
			end
		end
	end)```
1 Like

Question, is this script a local script inside your character, and the controls you cloning is it a screengui?

From what I got from the information you gave us, it’s a local script so I tried my best to fix it. When you parent the clone to the character you already had the character as a variable so you didn’t need to put b.Character, then there’s a undefined variable " occupier " so I made it a variable itself.

script.Parent.Changed:Connect(function()
	
	print("hiii")
	
	local a = game:GetService("Players").LocalPlayer
	
	local b = a.Character or a.CharacterAdded:Wait()
		
	if b then
		
		print("NOOOO")
		
		if not b.Parent:FindFirstChild("control") then
			
			local g = script:WaitForChild("control"):Clone()
			
			print("IT WORKED")
			
			g.Value.Value = script.Parent.Parent.Parent
			
			g.Parent= b
			
			g.Disabled = false
			
			local occupier = b
			
		end
		
	end
	
end)

Sorry I like spacing my code out to view it better.

1 Like

Which of the prints didn’t work?

the “IT WORKED” didn’t print the second time I used the machine gun.

that means control is already there. so try adding a :Remove() or :Destroy() of the control when you deselect the tool.

its not a tool, you sit on it to control it. And the script is removed

Give me more information and ill fix it, I need to know the properties

If you wanna see the model yourself, just search “turret” in the toolbox and use any of them. they all use the same script

Then what is the control, a screengui, model, folder?

a local script cloned into the character

So you have it like when the tool it parented inside player, it will clone controls, but when its not the controls go away

Yeah (i need to fill space lalalallaallalalalalalalalalalallalalalalalalalalallala)

script.Parent.Changed:Connect(function()

	print("hiii")

	local a = game:GetService("Players").LocalPlayer

	local b = a.Character or a.CharacterAdded:Wait()
	
	local g

	if b then

		if script.Parent == b then

			print("NOOOO")

			g = script:WaitForChild("control"):Clone()

			print("IT WORKED")

			g.Value.Value = script.Parent.Parent.Parent

			g.Parent= b

			g.Disabled = false

			local occupier = b
			
		elseif script.Parent == workspace then-- change this to the parent the tool going to be when its not in the character
			
			if g then
				
				g:Remove()
				
			end
			
		end

	end

end)

See if this work

im pretty sure I said several times its not a tool nor is it a local script, the “control” is a local script that gets cloned into the player when they sit on the machine gun and it is what allows them to opperate it.

Because this is false. In the first time using whatever this is, there’s no Instance called “control” in the character, and then it’s cloned and added in the character. When you use for the second time and check if there is not a child called “control”, it’ll be false because there is.

So what do you think i should do?

Maybe try deleting “control” at the end of the if statement