Hi! I've tried to make a code where my snow block can auto regenerate when it's not full. However, the descendant removing function only works in studio, but not in game. I've looked for errors but nothing appears.


local CollectionService = game:GetService("CollectionService")
local TaggedParts = CollectionService:GetTagged("SnowBlock")
local respawnCooldown = 5

for i, v in pairs(TaggedParts) do
	v.DescendantRemoving:Connect(function(part)
		print(part)
		print("hii")
		if part.Name ~= "Part6" and part.Name ~= "SnowBlock" then
			part:WaitForChild("Value").Parent = v:FindFirstChild("Part".. tostring(tonumber(string.sub(part.Name,5)) + 1))
			print("Part".. tostring(tonumber(string.sub(part.Name,5)) + 1))	
		end
	end)
	
	local function autoRespawn()
		while true do
			local backup = v:Clone()
			wait(respawnCooldown)
			for i, a in pairs(v:GetChildren()) do
				if a:FindFirstChild("Value") then
					if a.Name ~= "Part1" then
						backup.Parent = v
						for i, b in pairs(backup:GetChildren()) do
							if b.Name ~= a.Name then
								b:Destroy()
							else
								b.Name = "Part".. tostring(tonumber(string.sub(a.Name,5)) - 1)
								b.Parent = v
								b.CFrame = b.CFrame * CFrame.new(0,0.4,0)
								if not b:FindFirstChild("Value") then
									a:WaitForChild("Value").Parent = b
								else
									a:WaitForChild("Value"):Destroy()
								end
							end
						end
					end
					backup:Destroy()
				end
			end
		end
	end
	print("pinato")
	spawn(autoRespawn)
	--]]
end

7 comments

0

print(TaggedParts) and see in game if the table is not empty

0

Okay! I'll try that.

0

Nope! The table isn't empty, I've made sure to tag all of my models.

0

I found something out, when I switched it to a localscript it started working, but then the autorespawn function stopped working

0

Should I just put the auto-respawn inside a server script and use a bindable function to send the signal when a part is destroyed/has a new value?

0

i think u should rewrite it honestly. I read the first 9 lines then on the 10th I decided to stop reading it

0

I know, it's really messy, but it was the only way I could think of lmao.