Destroy/Clone Destroys once and keeps cloning

Hi,

In this game, parts are damaged on the game board during play and are refreshed by cloning a copy out of ServerStorage after a timed round. Parts to be damaged and replaced are located in a folder on the workspace and in RepStorage.

This script is run from ServerScriptStorage and is waiting for a change to a boolvalue in Replicated Storage. When the boolvalue is false it should destroy the Parts folder and then pull a clone out of ServerStorage to replace it and do it once per round. What I find is that it will perform this action once then after it will skip the destroy step but continue to clone the folder to the workspace every round. I’ve tried several iterations of this with waits, Debris, ect. and they all produce the same result. Also tried “while true do” but that crashes studio even with breaks and waits. It’s probably something simple but I’m not seeing it.

Thanks in advance,
D

local PPart = game.Workspace.PickupParts --Folder of parts on the gameboard
local PartReset = game.ServerStorage.PickupParts --Cloned replacement storage
local InRound = game.ReplicatedStorage.InRound --Round timer boolval t/f
local server = game:GetService("ServerStorage")

InRound.Changed:Connect(function(NewValue)

	if NewValue and InRound.Value == false then
		PPart:Destroy()
		wait (0.2)
		PartReset:Clone().Parent = game.Workspace
		print("PartReset")
	end
		if PPart.ChildAdded == true then	
		end
	print(PPart)
	
	end)

2nd Version:

local PPart = game.Workspace.PickupParts
local PartReset = game.ServerStorage.PickupParts
local InRound = game.ReplicatedStorage.InRound
local server = game:GetService("ServerStorage")

InRound.Changed:Connect(function(NewValue)
	
	local function ResetMap()
		PPart:Destroy()
		wait (5)
		local PartCopy = PartReset:Clone()
		PartCopy.Parent = game.Workspace
	end
while true do
if InRound.Value == false then
			ResetMap()
			wait(3)
			end
end
	end)
2 Likes

You could try:

while workspace:FindFirstChild("PPart")  do
	workspace.PPart:Destroy()
end
1 Like

Hey, I think I found your issue! You’re calling the PPart outside of a function, which means it only gets ‘looked at’ by the server once, whereas in a function, it would be checked twice. I did a bit of testing myself, and when I call my own part that is not in a function, it only destroys once and continuously, however when called in a function, it destroys and clones properly. I would also add a break so that the while true do function doesn’t continuously run, however if it breaks stuff (which it shouldn’t), it can be removed.

local PartReset = game.ServerStorage.PickupParts
local InRound = game.ReplicatedStorage.InRound
local server = game:GetService("ServerStorage")

InRound.Changed:Connect(function(NewValue)

	local function ResetMap()
		workspace:FindFirstChild("PickupParts"):Destroy()
		wait (5)
		local PartCopy = PartReset:Clone()
		PartCopy.Parent = game.Workspace
	end
	
	while true do
		if InRound.Value == false then
			ResetMap()
			wait(3)
			
			break
		end
	end
end)

Hope this helps!

5 Likes

Thank you both for taking the effort on this! I just learned something new. Your edit above did not work, it crashed studio when it got to the “while true do” loop. That was the 2nd version of my script with the function call. However, your logic about nesting the path of the object to be Destroyed worked perfectly in my 1st version.

local PartReset = game.ServerStorage.PickupParts --Cloned replacement storage
local InRound = game.ReplicatedStorage.InRound --Round timer boolval t/f checked/unchecked

InRound.Changed:Connect(function(NewValue) --Detect a change from round timer script boolval
	if InRound.Value == false then --Fires when in the intermission phase of the round
		game.Workspace.PickupParts:Destroy() --Folder of parts on the gameboard
		print("Destroy")
		wait (0.2)
		PartReset:Clone().Parent = game.Workspace--Cloned replacement
		print("PartReset")
	end
end)
2 Likes