Respawn script only works once

im trying to convert my npc stuff to collection service but for some odd reason it doesnt work again like the old code i used

Humanoid.Died:Connect(function()
		local ChosenWeapon = RarityModule:GetRandomSlot(MobInfo.Weapons)
		
		local ServerStorage = game.ServerStorage
		local Items = ServerStorage.Items
		local Weapon = Items[ChosenWeapon.Name]
		
		-- cloned weapon
		local ClonedWeapon = Weapon:Clone()
		ClonedWeapon.Parent = workspace
		ClonedWeapon.Handle.Position = NPCRootPart.Position
		
		local Tag = CollectionService:GetTagged(MobInfo.MobZone)
		local NPC = game.ServerStorage.NPC[MobInfo.MobZone][Humanoid.Parent.Name]
	    local MobFolder = workspace.MobHolder[MobInfo.MobZone]
		
		-- loop through then set position then destroy
		local Counter = 0
		
		for _, Part in pairs(Tag) do
			Counter = Counter + 1
			
			local CounterPart = math.random(1, Counter)
				
			NPC:Clone().Parent = MobFolder
			NPC:MoveTo(Tag[Counter].Position, Tag[Counter])
		        
			-- destroy it
			CollectionService:RemoveTag(NPCRootPart, Tag)
			Humanoid.Parent:Destroy()
			break
		end
		
	end)

i dont want separate scripts or it will have an impact on my game memory so yeah please help me fix it!

The :GetTagged function requests for a Tag name, in string, can you make sure that you are passing a String? Also could you tell if there are any errors or stuff?

MobInfo.MobZone = "StartingZone"

No errors specifically

Any more information on how the old code worked and what exact changes did you notice?

the old code is similar in function but in one zone if i were correct

I don’t think there would be any specifics changes because all you probably changed was getting parts tagged as “StartingZone” with CollectionService & then looping through them and doing the same function?

I would say you to check till where the code is actually running, and it might be that you haven’t tagged the parts as StartingZone?

the first attempt works and after that nothing

With first attempt do you mean the loop? Or the Humanoid.Died Connection?

a thing i did was remove the HumanoidRootPart from the table

yes the died connection event should i make it disconnectable? or what

The Humanoid.Died function will only connect to the current character. Instead, add a characteradded THEN a Humanoid.Died function. Like this:

player.CharacterAdded:Connect(function(char)
char.Humanoid.Died:Connect(function()
end)
end)

Although, this only works for players. I’m not sure whether you’re trying to make a respawn script for an NPC or the player.

2 Likes

sorry my guy this is NPC code that im showing rn not player code

Actually it still works in the same way I believe, because you will obviously Destroy the Dead NPC & Create a new one. So you still need to bind it again, if you aren’t doing already.

so disconnect it then make a new connection then?

Yeah, that should work. Not sure, but you can try that first.

nope nothing works wow almost any solution i try it doesnt work

I can see that your old code didn’t have the Remove Tag part, could you try removing it and then try?

Also, we don’t really know what most Variable values are and stuff, its pretty hard for us to debug your code without that, atleast for me it is, so you just need some patience to find the solution :slight_smile:

-- // Services \\ --
local CollectionService = game:GetService("CollectionService")
local NPCRootParts = CollectionService:GetTagged("NPCRootParts")
local ServerStorage = game:GetService("ServerStorage")

-- Debounce
local DAMAGE_DEBOUNCE = false
local Connection = nil
local MobDone = false

-- loop through if touched then damage them
for _, NPCRootPart in pairs(NPCRootParts) do
	if NPCRootPart:IsA("Part") then
		-- // Parental Objects \\ --
		local Humanoid = NPCRootPart.Parent.Humanoid
		
		-- // Modules
		local RarityModule = require(game.ReplicatedStorage.Shared.RarityModule)
		local MobInfo = require(Humanoid.Parent.MobInfo)
		
		-- // Events
		Connection = Humanoid.Died:Connect(function()
			local ChosenWeapon = RarityModule:GetRandomSlot(MobInfo.Weapons)
			
			local ServerStorage = game.ServerStorage
			local Items = ServerStorage.Items
			local Weapon = Items[ChosenWeapon.Name]
			
			-- cloned weapon
			local ClonedWeapon = Weapon:Clone()
			ClonedWeapon.Parent = workspace
			ClonedWeapon.Handle.Position = NPCRootPart.Position
			
			local Tag = CollectionService:GetTagged(MobInfo.MobZone)
			local NPC = game.ServerStorage.NPC[MobInfo.MobZone][Humanoid.Parent.Name]
			local MobFolder = workspace.MobHolder[MobInfo.MobZone]
			
			-- loop through then set position then destroy
			local Counter = 0
			
			for _, Part in pairs(Tag) do
				Counter = Counter + 1
				
				local CounterPart = math.random(1, Counter)
				
				NPC:Clone().Parent = MobFolder
				NPC:MoveTo(Tag[Counter].Position, Tag[Counter])
				
				-- destroy it
				pcall(function()
					Connection:Disconnect()
					Connection = nil
					MobDone = true
				end)
				CollectionService:RemoveTag(NPCRootPart, Tag)
				Humanoid.Parent:Destroy()
				break
			end
			
		end)
	end
	
	if MobDone then
		break
	end
end

there u go the code that u asked for i guess