Issues with cloning and maintaing mouseclicked event

Currently having an issue with cloning my walls and them keeping the mouseclicked event once cloned. My walls are initially starting in workspace, however once a local script detects that the walls are at 0 health, it deletes the wall locally for the player. After destroying the wall, the player then steps on a platform that gives the player a win, teleports them back to the start, and clones the walls again locally so the player can continue breaking the walls and obtaining more wins. However after i destroyed the first walls and the new walls are cloned from replicated storage, i cannot trigger the mouseclicked event. All the children (including the click detector) are cloned inside, and when i hover my mouse over it changes the icon to signify a click detector inside. It’s just when i click it, it doesn’t trigger the event.

Server Script:

task.wait(1)
local wall1 = game.Workspace.Map1Walls:WaitForChild("Wall1")
local wall2 = game.Workspace.Map1Walls:WaitForChild("Wall2")



wall1.ClickDetector.MouseClick:Connect(function(plr)
	print("Clicked")
	local damage = plr.leaderstats.Fruit
	local number = damage.Value
	
	local humanoid = plr.Character:WaitForChild("Humanoid")
	local animate = humanoid:WaitForChild("Animator")
	local kickAnimation = Instance.new("Animation")
	kickAnimation.AnimationId = "rbxassetid://16826458454"
	local kickAnimationTrack = animate:LoadAnimation(kickAnimation)
	kickAnimationTrack:Play()
	
	
	local health = wall1.Health
	local health1 = game.ReplicatedStorage.Health1
	health1:FireClient(plr)
	
	
	
end)


wall2.ClickDetector.MouseClick:Connect(function(plr)
	print("Clicked")
	local damage = plr.leaderstats.Fruit
	local number = damage.Value
	
	local humanoid = plr.Character:WaitForChild("Humanoid")
	local animate = humanoid:WaitForChild("Animator")
	local kickAnimation = Instance.new("Animation")
	kickAnimation.AnimationId = "rbxassetid://16826458454"
	local kickAnimationTrack = animate:LoadAnimation(kickAnimation)
	kickAnimationTrack:Play()
	local health = wall2.Health
	local health2 = game.ReplicatedStorage.Health2
	health2:FireClient(plr)

	
	
end)

Local Scripts:
Wall damage:

local health1 = game.ReplicatedStorage.Health1
local health2 = game.ReplicatedStorage.Health2



health1.OnClientEvent:Connect(function()
	local wall1 = game.Workspace.Map1Walls:WaitForChild("Wall1")
	local health = wall1.Health
	local damage = game.Players.LocalPlayer.leaderstats.Fruit
	local number = damage.Value

	health.Value = health.Value - number
	
	if health.Value <= 0 then
		local wall1 = game.Workspace.Map1Walls:FindFirstChild("Wall1")
		wall1:Destroy()
	

	end
	
	
end)

health2.OnClientEvent:Connect(function()
	local wall1 = game.Workspace.Map1Walls:WaitForChild("Wall2")
	local health = wall1.Health
	local damage = game.Players.LocalPlayer.leaderstats.Fruit
	local number = damage.Value

	health.Value = health.Value - number
	if health.Value <= 0 then	
		local wall2 = game.Workspace.Map1Walls:FindFirstChild("Wall2")
		wall2:Destroy()

		



	end


end)

Win giver and clone script:

task.wait(5)
local winbutton = game.Workspace.Win:WaitForChild("WinButton")
local teleport1 = game.Workspace:WaitForChild("Map1Teleport")



winbutton.Touched:Connect(function(plr)

	local humanoid = plr.Parent:FindFirstChild("HumanoidRootPart")
	if plr.Name == "HumanoidRootPart" then

		local player = game.Players:GetPlayerFromCharacter(plr.Parent)
		player.leaderstats.Wins.Value += 1
		local HRP = plr:FindFirstChild("HumanoidRootPart")
		plr:PivotTo(teleport1.CFrame)
		winbutton.CanTouch = false		
		local clonewall1 = game.ReplicatedStorage.Walls.Map1Walls.Wall1
		local clonewall2 = game.ReplicatedStorage.Walls.Map1Walls.Wall2
		clonewall1:Clone()
		clonewall2:Clone()
		clonewall1.Parent = game.Workspace.Map1Walls
		clonewall2.Parent = game.Workspace.Map1Walls

		task.wait(0.1)
		winbutton.CanTouch = true
	end




end)

2 Likes

Cloning does not keep connections, you’ll have to redo them

use plr:GetMouse() to get the players mouse

then you can detect mouse clicks and use mouse.Target to get what the mouse is clicking on

now you only need one connection and it works for every wall and cloned wall

I’m trying to instance.new a click detector each time it’s cloned, still doesnt work :confused:

The wall already has the click detector when you clone it, just use the same function for the on clicked event