Does anyone know why my part respawn script only works once?

it should happen every 5 seconds (5 seconds for testing, also cd.Value)

local Physics = game.ReplicatedStorage:WaitForChild("PhysicsWall")
local cd = script:WaitForChild("Cooldown")
local count = 5

local function respawn()
	if game.Workspace:FindFirstChild("PhysicsWall") then
		game.Workspace.PhysicsWall:Destroy()
	end
	wait(1)
	Physics:Clone().Parent = workspace
	for i, v in pairs(Physics:GetDescendants()) do
		if v:IsA("BasePart") then
			v.Anchored = true
			wait(2)
			v.Anchored = false
		end
	end

end

while task.wait(cd.Value) do
	for i = 1, 5 do
		print("Parts respawning in " .. count)
		count -= 1
		task.wait(1)
	end
	respawn()
end

maybe its because its located in the workspace
im not sure

1 Like

Better to use task.wait

After that better to set count to 5 again.

Your code works for me

where did you place the script?

ServerScriptService.

Also, he could use attributes instead of IntValue

Edit: It works even with localscript

And he placed the script in Workspace. I guess it only works on ServerScriptService because server scripts are meant to be there anyways, and not Workspace.

And localscript would only spawn stuff for the client

Its currently in the workspace

Im gonna come back to this after i get back from school

1 Like

I fixed your script. I removed the value cd that you had and I placed this script in ServerScriptService so that way it handle it on the server side.

local Physics = game.ReplicatedStorage:WaitForChild("PhysicsWall")
local countdownTime = 5
local respawnCooldown = 10 -- Replace this with the desired cooldown time

local function respawn()
	-- Destroy existing PhysicsWall in Workspace if it exists
	if game.Workspace:FindFirstChild("PhysicsWall") then
		game.Workspace.PhysicsWall:Destroy()
	end

	-- Wait before respawning
	wait(1)

	-- Clone PhysicsWall from ReplicatedStorage and parent it to Workspace
	local clonedPhysics = Physics:Clone()
	clonedPhysics.Parent = workspace

	-- Anchor and then unanchor parts within the PhysicsWall
	for i, v in pairs(clonedPhysics:GetDescendants()) do
		if v:IsA("BasePart") then
			v.Anchored = true
			wait(2)
			v.Anchored = false
		end
	end
end

-- Main loop to handle the cooldown and respawn process
while task.wait(respawnCooldown) do
	local count = countdownTime
	for i = 1, countdownTime do
		print("Parts respawning in " .. count)
		count -= 1
		task.wait(1)
	end
	respawn()
end

1 Like

ill have a look and see if this works correctly

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