Model not working after clone

Hello ,

This maybe my 2nd topic . Anyway I ran into problem that whenever my model get cloned the script dosn’t work at the model . I use ChildAdded method but still same thing happening . Also the script is in the serverscriptservice .

workspace.Map.Mob.ChildAdded:Connect(function()



				Checkplayer()

				Touched()


			end)

Am I doing it right or not ?? . Any help is appreciated :smile:

Can you give more context/information? What does Touched or Checkplayer do?

1 Like

U mean this ??

local function Touched()
			
			local deb = false

			for parent , child in pairs(mob) do
				if (root.Position - child.PrimaryPart.Position).Magnitude < killdis and hum.Health > 0 then
					
					if deb == false then
						
						deb = true
						
						local animationtrack = child.Humanoid.Animator:LoadAnimation(script.Animation)
						animationtrack:Play()

						wait(animationtrack.Length)

						hum:TakeDamage(5)

						animationtrack:Stop()
						animationtrack:Destroy()
						
						deb = false
						
					else print(deb) end
			
					
				else Checkplayer() end
					
					
			    end 
		end

local function Checkplayer()
			
			for parent , child in pairs(mob) do
				
				if (root.Position - child.PrimaryPart.Position).Magnitude <= dis then

					child.Humanoid:MoveTo(root.Position)

				else
					print("bruh")
				end
			end	
		end

Yes; but I need all of your server script. It’s important so that I and others can debug your code and figure out what’s wrong, i.e mob is an unknown variable.

1 Like

First thing first Im sorry cuz my script is very long . Second my script is quiet messy

local con

local datastore2 = require(game.ReplicatedStorage.Module.MainModule)
local mobmodule = require(game.ReplicatedStorage.Module.LootPlan)
local FastWait = require(game.ReplicatedStorage.Module.FastWait)

local newLoot = mobmodule.new("single")

newLoot:AddLoot("Bandits" , 30)
newLoot:AddLoot("Glein" , 25)
newLoot:AddLoot("Goldem" , 1)
newLoot:AddLoot("Bear" , 20)
newLoot:AddLoot("Chicken" , 50)



local mob = workspace.Map.Mob:GetChildren()
local SPAWN = game.Workspace.Map.MobSpawner:GetChildren()
local rndom = SPAWN[math.random(1 , #SPAWN)]

local Hit_Table = {}

local Mob_Rank = {
	
	["Bandits"] = 50;
	["Glein"]   = 20;
	["Goldem"]  = 60;
	["Bear"]    = 10;
	["Chicken"] = 5
}


con = game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		
		
		local Key = datastore2("CoinKey" , player)
		local Key2 = datastore2("LevelKey" , player)
		local Key3 = datastore2("ExpKey" , player)
		local key4 = datastore2("MaxEspKey" , player)
		
		local hum = char:WaitForChild("Humanoid")
		local root = char:WaitForChild("HumanoidRootPart")
		local dis = 15
		local killdis = 2
		
		
	   local function moverandom()
			
		end
		
		local function Checkplayer()
			
			for parent , child in pairs(mob) do
				
				if (root.Position - child.PrimaryPart.Position).Magnitude <= dis then

					child.Humanoid:MoveTo(root.Position)

				else
					coroutine.resume(coroutine.create(moverandom))
				end
			end	
		end
		
		local function CheckMob()
			

			local child = mob

			local count = #mob
			
			if count < 20 then
				
				for i = 1 , 20 - count do
					
					local counter = newLoot:GetRandomLoot(1)

					local newmob = game.ReplicatedStorage.Mob:FindFirstChild(counter)

					if newmob then

						local clone = newmob:Clone()
						clone.Parent = game.Workspace.Map.Mob
						clone.PrimaryPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame * Vector3.new(10,10,10))
						print(counter)

					else
						return
					end
					
					
				end
				
			print(count)
			end
		end
		
		local function Touched()
			
			local deb = false

			for parent , child in pairs(mob) do
				if (root.Position - child.PrimaryPart.Position).Magnitude < killdis and hum.Health > 0 then
					
					if deb == false then
						
						deb = true
						
						local animationtrack = child.Humanoid.Animator:LoadAnimation(script.Animation)
						animationtrack:Play()

						wait(animationtrack.Length)

						hum:TakeDamage(5)

						animationtrack:Stop()
						animationtrack:Destroy()
						
						deb = false
						
					else print(deb) end
			
					
				else Checkplayer() end
					
					
			    end 
		end
		
		local function Refresh()
			
			while true do
				
				coroutine.create(function()
				
				Checkplayer()

				Touched()
			
				end)
				
				FastWait(0.05)
				
			end
			
		end
		
		local function Dead(corpse)
			
			FastWait()
			
			corpse:Destroy()
			
			FastWait(math.random(1, 3))
			
			local counter = newLoot:GetRandomLoot(1)

			local newmob = game.ReplicatedStorage.Mob:FindFirstChild(counter)

			if newmob then

				local clone = newmob:Clone()
				clone.Parent = game.Workspace.Map.Mob
				clone.PrimaryPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame * Vector3.new(10,10,10))
				print(counter)
				
				Refresh()
				
			end
			
		end
		
		for i , v in pairs(mob) do
			v.Humanoid.Died:Connect(function()

				local tag = v.Humanoid:FindFirstChild("creator")

				if tag ~= nil then
					local killer = tag.Value
					print(killer)

				else

					local tag = Instance.new("ObjectValue" , v.Humanoid)
					tag.Name = 'creator'
					tag.Value = player

					game:GetService("Debris"):AddItem(tag , 0.25)
					print(tag.Value)
						
				end
				
				Dead(v)

			end)
		end
		
	
		coroutine.resume(Refresh())
		
		workspace.Map.Mob.ChildAdded:Connect(function()
			
			coroutine.resume(Refresh())
			
		end)
		
		
	end)
end)

pls dont crictize me … ik and Im still learning :sweat_smile:

I immediately spotted the issue. You never update local mob = workspace.Map.Mob:GetChildren(), which means that new children will not be added to that mob table.
Instead, try updating only the new children. As an example:

local enemies = {}
local function EnemyAdded(enemy)
    table.insert(enemies, enemy)
end

workspace.Map.Mob.ChildAdded:Connect(EnemyAdded)
for _, enemy in ipairs(workspace.Map.Mop:GetChildren()) do
    local Success, Error = pcall(coroutine.wrap(EnemyAdded))
    ...
end
while true do
    coroutine.resume(Checkplayer)
    coroutine.resume(Touched)

    FastWait(0.05)
end

I’m not saying this is explicitly how you should handle it; just a way I would.

Sidenote: You’re using coroutine.resume() wrong for Refresh(). It’s supposed to be coroutine.resume(Refresh), assuming that Refresh is a coroutine. If it’s not, do make sure that Refresh is a coroutine, like so:

local Refresh = coroutine.create(function()
		while true do
			Checkplayer()
			Touched()
				
			FastWait(0.05)
		end
end)

This will make sure that your code does not indefinitely yield/never proceed with code AFTER Refresh() has ran.

One questions… why do you put

    coroutine.resume(Checkplayer)
    coroutine.resume(Touched)

I didn’t use coroutine on those two function

It’s an assumption in your hypothetical code example that both functions were converted to Coroutines.
You can do the same thing except spawning new coroutines by just doing coroutine.resume(coroutine.create(Checkplayer)) instead.