[SOLVED]How do I stop humanoids cloning more than once?

Hello,
I’m having trouble working on a script that has the player place a building down.
While the building placement is functional, the problem is that even after placing the building down,
if I click on another spot on the baseplate, it moves the building to the exact area I clicked on.
It also clones more humanoids than I needed to be cloned.
I have tried more than seven potential solutions to fix this, but its to no avail.
How do the game from moving my building to another area upon clicking?
and how do I stop the game from cloning too many humanoids?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer
local screengui = script.Parent
local buildbutton = screengui.Build
local researchbutton = screengui.Research
local Mouse = Player:GetMouse()


researchbutton.MouseButton1Click:Connect(function()
	if researchbutton.Frame.Visible == false then
		researchbutton.Frame.Visible = true
	else 
		researchbutton.Frame.Visible = false
	end
end)



buildbutton.MouseButton1Click:Connect(function()
	
	if buildbutton.Frame.Visible == false then
		buildbutton.Frame.Visible = true
	else 
		buildbutton.Frame.Visible = false
	end
	
	local totembutton = buildbutton.Frame.Totem
	
	totembutton.MouseButton1Click:Connect(function()
		local totem = ReplicatedStorage.NeolithicTotem
		local totemclone_marker = totem:Clone()
		totemclone_marker.Parent = workspace
		totemclone_marker.Material = Enum.Material.Neon
		totemclone_marker.BrickColor = BrickColor.new("Neon green")
		totemclone_marker.Transparency = 0.7
		
		
		if totemclone_marker.Parent == workspace then
			
			Mouse.Move:Connect(function()
				local MousePosX = Mouse.Hit.Position.X
				local MousePosY = Mouse.Hit.Position.Y
				local MousePosZ = Mouse.Hit.Position.Z
				totemclone_marker.CFrame = CFrame.new(MousePosX, math.clamp(MousePosY, 2, 10), MousePosZ)
				
				
				
				
				if buildbutton.Frame.Visible == false then
					totemclone_marker:Destroy()		
				end
			end)
			
			local istotem_cloned = false
			
			if buildbutton.Frame.Totem.Visible == true then
				Mouse.Button1Down:Connect(function()
					istotem_cloned = true
					totem.Parent = workspace
					totem.CanCollide = true
					totem.Anchored = true
					
					totem.CFrame = CFrame.new(totemclone_marker.CFrame.Position)
					if istotem_cloned == true then
						
						totembutton.Active = false
						totembutton.Visible = false
						totemclone_marker:Destroy()
						
						if totem.Parent == workspace then
							local hominid = ReplicatedStorage.Hominid
							local hominidclone = hominid:Clone()
							local hominidclone2 = hominid:Clone()
							local hominidclone3 = hominid:Clone()
							local hominidclone4 = hominid:Clone()
							
						
							
							hominidclone.Parent = workspace
							hominidclone2.Parent = workspace
							hominidclone3.Parent = workspace
							hominidclone4.Parent = workspace
							
							hominidclone.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(-4,5,3))
							hominidclone2.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(-6,5,-3))
							hominidclone3.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(4, 5, -3))
							hominidclone4.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(-6, 5, 3))
							
							istotem_cloned = false
							
					
						end
					end
				end)
			end
			
		end
	end)	
end)

Any potential help, insights, or clues are greatly appreciated.
Thanks!

2 Likes

You haven’t added any type of cool-down, check or anything of the sort to stop infinite placements. You should add a check to see if these placements exist.

I did try adding checks and debounces to the script
but it still doesnt seem to solve the issue.
The problem is that I’m not sure how i can check if these instances exist without destroying the instances I need in the game. Is there a way i can check if there are more than the desired amount of instances i need without destroying all of them and only the excess clones?

You could possibly have a Folder that the Things you want to check go to, and then do:

if #workspace.Stuff:GetChildren() < 5 then --// checks if less than 5 

end 
1 Like

Hmm, good idea
I’ll try it out
but I’m afraid even with this check in place,
it will still count for all of the clones, even the ones i want to exist - to be destroyed.
What I can add to this is getting the game to put the unwanted clones in a folder named “excess” or somn like that, and ONLY destroy those within that folder, leaving the only clones i want in the game intact.

If its done correctly, more above what you want to spawn won’t.

You have a memory leak here which is causing clicks for the totembutton to multiply

Edit: @Astrosupernuat There’s one here too:

1 Like

I did try and work with this potential solution, but it says the following error:

It doesnt like that I’m trying to compare a number to a table.
I guess I cant quantify using :GetChildren(), though it should be an option.
Unless i’m doing something incorrect with it?

if workspace.Assets.Hominids:GetChildren() > 4 then
	hominid.Parent = workspace.Assets.Excess
								
	if workspace.Assets.Excess:GetChildren() > 0 then
	   workspace.Assets.Excess:ClearAllChildren()
	end
end

you didnt add the # at the end.

if #workspace.Assets.Hominids:GetChildren() > 4 then
	hominid.Parent = workspace.Assets.Excess
								
	if #workspace.Assets.Excess:GetChildren() > 0 then
	   workspace.Assets.Excess:ClearAllChildren()
	end
end

This should fix the memory leaks by the way:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer
local screengui = script.Parent
local buildbutton = screengui.Build
local researchbutton = screengui.Research
local Mouse = Player:GetMouse()

local totembutton = buildbutton.Frame.Totem

local mouseMoveConnection, mouseClickConnection

researchbutton.MouseButton1Click:Connect(function()
	if researchbutton.Frame.Visible == false then
		researchbutton.Frame.Visible = true
	else 
		researchbutton.Frame.Visible = false
	end
end)

buildbutton.MouseButton1Click:Connect(function()
	if buildbutton.Frame.Visible == false then
		buildbutton.Frame.Visible = true
	else 
		buildbutton.Frame.Visible = false
	end
end)

totembutton.MouseButton1Click:Connect(function()
	local totem = ReplicatedStorage.NeolithicTotem
	local totemclone_marker = totem:Clone()
	totemclone_marker.Parent = workspace
	totemclone_marker.Material = Enum.Material.Neon
	totemclone_marker.BrickColor = BrickColor.new("Neon green")
	totemclone_marker.Transparency = 0.7


	if totemclone_marker.Parent == workspace then
		if mouseMoveConnection then return end

		mouseMoveConnection = Mouse.Move:Connect(function()
			local MousePosX = Mouse.Hit.Position.X
			local MousePosY = Mouse.Hit.Position.Y
			local MousePosZ = Mouse.Hit.Position.Z
			totemclone_marker.CFrame = CFrame.new(MousePosX, math.clamp(MousePosY, 2, 10), MousePosZ)




			if buildbutton.Frame.Visible == false then
				totemclone_marker:Destroy()		
			end
		end)

		local istotem_cloned = false

		if buildbutton.Frame.Totem.Visible == true then
			if mouseClickConnection then return end

			mouseClickConnection = Mouse.Button1Down:Connect(function()
				istotem_cloned = true
				totem.Parent = workspace
				totem.CanCollide = true
				totem.Anchored = true

				totem.CFrame = CFrame.new(totemclone_marker.CFrame.Position)
				if istotem_cloned == true then

					totembutton.Active = false
					totembutton.Visible = false
					totemclone_marker:Destroy()

					if totem.Parent == workspace then
						local hominid = ReplicatedStorage.Hominid
						local hominidclone = hominid:Clone()
						local hominidclone2 = hominid:Clone()
						local hominidclone3 = hominid:Clone()
						local hominidclone4 = hominid:Clone()



						hominidclone.Parent = workspace
						hominidclone2.Parent = workspace
						hominidclone3.Parent = workspace
						hominidclone4.Parent = workspace

						hominidclone.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(-4,5,3))
						hominidclone2.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(-6,5,-3))
						hominidclone3.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(4, 5, -3))
						hominidclone4.PrimaryPart.CFrame = CFrame.new(totem.Position + Vector3.new(-6, 5, 3))

						istotem_cloned = false


					end
				end
			end)
		else
			if mouseClickConnection then
				mouseClickConnection:Disconnect()
				mouseClickConnection = nil
			end
		end
	else
		if mouseMoveConnection then
			mouseMoveConnection:Disconnect()
			mouseMoveConnection = nil
		end
	end
end)	

Edit: @Astrosupernuat As another precausion I’ve just added guard statements to make sure the connections aren’t accidentally recreated even if the parent stays the same

1 Like

Probably, I only offered a half solution based off the message he said.

2 Likes

Ohhhhh
I didn’t know that was required.
It does work tho!
However, if i still click another place, it still spawns an extra set of humanoids
before completely destroying any further instances.
It also removes the original single instance from the ReplicatedStorage somehow?

I will test that out and see if it works.
Also, how do you know that’s a memory leak?

1 Like

Currently whenever the buildbutton is clicked a new connection is made for the totembutton, which won’t disconnect any previous connections made for that button. This will cause clicks to start registering multiple times for the totembutton instead of just once

1 Like

Okay, so I did try out your code
and it doesnt seem to show any improvement so far.
It still duplicates the humanoids more than I want it to
and the totem still changes position to wherever area i clicked on
This is quite a tricky bug to solve.
No errors in the console, just that it still continues the same error.

@dfgfdgfdsyhdgfyh 's solution seems to partially solve it but
it somehow destroys the single instance within ReplicatedStorage.

1 Like

Doing this should skip Instances that are inside of ReplicatedStorage when destroying them:

if #workspace.Assets.Hominids:GetChildren() > 4 then
	hominid.Parent = workspace.Assets.Excess

	if #workspace.Assets.Excess:GetChildren() > 0 then
		for _, instance in workspace.Assets.Excess:GetChildren() do
			if instance:IsDescendantOf(ReplicatedStorage) then continue end

			instance:Destroy()
		end
	end
end
1 Like

Still no improvement so far lol
It somehow just includes the one in ReplicatedStorage to be terminated?
Maybe If we somehow exclude the one in ReplicatedStorage through a condition, perhaps it wont focus on that instance anymore.
And there should be a way to get rid of that extra set of humanoids that spawn in whenever i click somewhere else.

If it comes down to it, I might have to re-write the whole thing.

I suspect something else is deleting the one inside of ReplicatedStorage because at this point there doesn’t seem to be anything in the scripts that might be causing it to happen

I think I found why it does this.

local hominid = ReplicatedStorage.Hominid

if its not within the hominids folder in the workspace, or i guess that if there are just overall more than 4 in the game, it completely destroys all other instances, including the one within ReplicatedStorage.
That’s just my theory, but my guess is this is prolly a referencing issue.

1 Like

How many hominids can exist inside of the Assets.Hominids folder and Assets.Excess folder before they’re destroyed?

1 Like