Model not respawning

Hey all. My game has a script to respawn an important model if the player drags it out of the map. In some occasions, the model just won’t respawn. The tricky part is that the print at the bottom still appears in the console. I’m probably just bad at coding.
I have a very similar function for a different model, and my players say that works just fine.

local function newBodyClone(body)
	-- ✅ Stop if the mode doesn't allow bodies
	if teleportData and teleportData.otherData and teleportData.otherData.Bodies and string.match(teleportData.otherData.Bodies, "Bodies") == nil then
		return
	end

	-- ✅ Only add the body if it's not already in the table
	if not table.find(bodies, body) then
		table.insert(bodies, body)
	end

	-- ✅ Ensure each body has its own event listener
	body:GetPropertyChangedSignal("Parent"):Connect(function()
		if body.Parent == nil then
			task.wait(0.1) -- Short delay to prevent infinite loops

			-- ✅ Remove the old body from tracking
			for i, trackedBody in ipairs(bodies) do
				if trackedBody == body then
					table.remove(bodies, i)
					break
				end
			end

			-- ✅ Clone a new body at the original location
			local newClone = body:Clone()
			newClone.Parent = map
			table.insert(bodies, newClone) -- Track the new clone properly

			-- ✅ Recursively apply tracking to the new clone
			newBodyClone(newClone) 

			print("A body flew out of the map! Respawning a clone.")
		end
	end)
end

Second function if it helps:

local function newWeaponClone(weapon)
	-- Prevent duplicate tracking of the same weapon
	if weapon:GetAttribute("IsCloned") then return end
	weapon:SetAttribute("IsCloned", true)
	
	local clone = weapon:Clone()

	-- Replace the weapon if it gets deleted
	weapon:GetPropertyChangedSignal("Parent"):Connect(function()
		if weapon.Parent == nil then
			task.wait(0.1) -- Small delay to prevent recursion loop
			clone.Parent = weapons
			newWeaponClone(clone) -- Track the new clone
		end
	end)
end 

YES CHATGPT HELPED ME

chat im such an idiot i need to make the clone before the parent changes. just like the weapon function. ill try it out

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.