How can I restart a function when it was completed

Hello everyone, I made a collector, and when a player finished to collect all the parts I want the player to restart the collector system again.

Here is the script:


local Players = game.Players.LocalPlayer

wait(1)
container.Parent = game.Workspace
local collected = 0
for i, v in pairs(container:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function(player)
		collected = collected + 1
		game.Workspace.Sounds.Grabbed.Playing = true
		v:Destroy()

		local CollectedText = script.Parent

		if collected == 1 then
			CollectedText.Text = "1/15 Containers"

		elseif collected == 2 then
			CollectedText.Text = "2/15 Containers"

		elseif collected == 3 then
			CollectedText.Text = "3/15 Containers"

		elseif collected == 4 then
			CollectedText.Text = "4/15 Containers"

		elseif collected == 5 then
			CollectedText.Text = "5/15 Containers"

		elseif collected == 6 then
			CollectedText.Text = "6/15 Containers"

		elseif collected == 7 then
			CollectedText.Text = "7/15 Containers"

		elseif collected == 8 then
			CollectedText.Text = "8/15 Containers"

		elseif collected == 9 then
			CollectedText.Text = "9/15 Containers"

		elseif collected == 10 then
			CollectedText.Text = "10/15 Containers"

		elseif collected == 11 then
			CollectedText.Text = "11/15 Containers"

		elseif collected == 12 then
			CollectedText.Text = "12/15 Containers"

		elseif collected == 13 then 
			CollectedText.Text = "13/15 Containers"

		elseif collected == 14 then
			CollectedText.Text = "14/15 Containers"

		elseif collected == 15 then
			game.Workspace.Sounds.Reward.Playing = true
			CollectedText.TextColor3 = Color3.new(0, 0.976806, 0)
			CollectedText.Text = "15/15 Containers"
			wait(2)
			CollectedText.Parent:Destroy()
			wait(0.01)
			local HumanoidRootPart = player.Character.HumanoidRootPart
			HumanoidRootPart.CFrame = game.Workspace.End.CFrame
		end
		print("Got it!")
	end)
end

repeat wait() until collected == 15
print("Containers were found")

I hope you can help me, thank your for your time

I believe you could give this a try:

local Players = game.Players.LocalPlayer

wait(1)
container.Parent = game.Workspace
function startCollecter()
	local collected = 0
	for i, v in pairs(container:GetChildren()) do
		v.ClickDetector.MouseClick:Connect(function(player)
			collected = collected + 1
			game.Workspace.Sounds.Grabbed.Playing = true
			v:Destroy()
	
			local CollectedText = script.Parent
	
			if collected == 1 then
				CollectedText.Text = "1/15 Containers"
	
			elseif collected == 2 then
				CollectedText.Text = "2/15 Containers"
	
			elseif collected == 3 then
				CollectedText.Text = "3/15 Containers"
	
			elseif collected == 4 then
				CollectedText.Text = "4/15 Containers"
	
			elseif collected == 5 then
				CollectedText.Text = "5/15 Containers"
	
			elseif collected == 6 then
				CollectedText.Text = "6/15 Containers"
	
			elseif collected == 7 then
				CollectedText.Text = "7/15 Containers"
	
			elseif collected == 8 then
				CollectedText.Text = "8/15 Containers"
	
			elseif collected == 9 then
				CollectedText.Text = "9/15 Containers"
	
			elseif collected == 10 then
				CollectedText.Text = "10/15 Containers"
	
			elseif collected == 11 then
				CollectedText.Text = "11/15 Containers"
	
			elseif collected == 12 then
				CollectedText.Text = "12/15 Containers"
	
			elseif collected == 13 then 
				CollectedText.Text = "13/15 Containers"
	
			elseif collected == 14 then
				CollectedText.Text = "14/15 Containers"
	
			elseif collected == 15 then
				game.Workspace.Sounds.Reward.Playing = true
				CollectedText.TextColor3 = Color3.new(0, 0.976806, 0)
				CollectedText.Text = "15/15 Containers"
				wait(2)
				CollectedText.Parent:Destroy()
				wait(0.01)
				local HumanoidRootPart = player.Character.HumanoidRootPart
				HumanoidRootPart.CFrame = game.Workspace.End.CFrame
			end
			print("Got it!")
		end)
	end
    repeat wait() until collected == 15
    print("Containers were found")
    startCollecter()
end

startCollecter()
1 Like

okay, i will try it, thank you

Actually, on a second though. When you click the collected parts, do you delete them? If you delete them, this will not work. You will need to respawn them, but I can help with that as well.

1 Like

Also instead of doing

if collected == 1 then
			CollectedText.Text = "1/15 Containers"

		elseif collected == 2 then
			CollectedText.Text = "2/15 Containers"

		elseif collected == 3 then
			CollectedText.Text = "3/15 Container
        ...

You could do:

CollectedText.Text = tostring(collected).."/15 Containers"
1 Like

yes, I deleted them, how could I respawn them?

oh alright, this for more short, right?

1 Like

@Daronion


print("Containers were found")

startCollecter()

container:Clone()

container.Parent = game.Workspace

end

startCollecter()

should I try this?

You can try this:

local Players = game.Players.LocalPlayer

wait(1)
container.Parent = game.ReplicatedStorage
function startCollecter()
    local container = container:Clone()
    container.Parent = workspace
	local collected = 0
	for i, v in pairs(container:GetChildren()) do
		v.ClickDetector.MouseClick:Connect(function(player)
			collected = collected + 1
			game.Workspace.Sounds.Grabbed.Playing = true
			v:Destroy()
	
			local CollectedText = script.Parent
	
			if collected == 1 then
				CollectedText.Text = "1/15 Containers"
	
			elseif collected == 2 then
				CollectedText.Text = "2/15 Containers"
	
			elseif collected == 3 then
				CollectedText.Text = "3/15 Containers"
	
			elseif collected == 4 then
				CollectedText.Text = "4/15 Containers"
	
			elseif collected == 5 then
				CollectedText.Text = "5/15 Containers"
	
			elseif collected == 6 then
				CollectedText.Text = "6/15 Containers"
	
			elseif collected == 7 then
				CollectedText.Text = "7/15 Containers"
	
			elseif collected == 8 then
				CollectedText.Text = "8/15 Containers"
	
			elseif collected == 9 then
				CollectedText.Text = "9/15 Containers"
	
			elseif collected == 10 then
				CollectedText.Text = "10/15 Containers"
	
			elseif collected == 11 then
				CollectedText.Text = "11/15 Containers"
	
			elseif collected == 12 then
				CollectedText.Text = "12/15 Containers"
	
			elseif collected == 13 then 
				CollectedText.Text = "13/15 Containers"
	
			elseif collected == 14 then
				CollectedText.Text = "14/15 Containers"
	
			elseif collected == 15 then
				game.Workspace.Sounds.Reward.Playing = true
				CollectedText.TextColor3 = Color3.new(0, 0.976806, 0)
				CollectedText.Text = "15/15 Containers"
				wait(2)
				CollectedText.Parent:Destroy()
				wait(0.01)
				local HumanoidRootPart = player.Character.HumanoidRootPart
				HumanoidRootPart.CFrame = game.Workspace.End.CFrame
			end
			print("Got it!")
		end)
	end
    repeat wait() until collected == 15
    container:Destroy()
    print("Containers were found")
    startCollecter()
end

startCollecter()
1 Like

ok this is a super bad habit, instead you should try this:

if collected < 15 then
    CollectedText.Text = tostring(collected).."/15 Containers"
else
    game.Workspace.Sounds.Reward.Playing = true
    CollectedText.TextColor3 = Color3.new(0, 0.976806, 0)
    CollectedText.Text = "15/15 Containers"
    wait(2)
    CollectedText.Parent:Destroy()
    wait(0.01)
    local HumanoidRootPart = player.Character.HumanoidRootPart
    HumanoidRootPart.CFrame = game.Workspace.End.CFrame
end

See much shorter!!!

@Daronion the parts that are needed to collect it seems to appear when you clicked, I mean they appear on color gray and when you clicked them it appears normal

here:

Interesting. Can you maybe try this, just a naming change:

local Players = game.Players.LocalPlayer

wait(1)
container.Parent = game.ReplicatedStorage
function startCollecter()
    local newContainer = container:Clone()
    newContainer.Parent = workspace
	local collected = 0
	for i, v in pairs(newContainer:GetChildren()) do
		v.ClickDetector.MouseClick:Connect(function(player)
			collected = collected + 1
			game.Workspace.Sounds.Grabbed.Playing = true
			v:Destroy()
	
			local CollectedText = script.Parent
	
			if collected == 1 then
				CollectedText.Text = "1/15 Containers"
	
			elseif collected == 2 then
				CollectedText.Text = "2/15 Containers"
	
			elseif collected == 3 then
				CollectedText.Text = "3/15 Containers"
	
			elseif collected == 4 then
				CollectedText.Text = "4/15 Containers"
	
			elseif collected == 5 then
				CollectedText.Text = "5/15 Containers"
	
			elseif collected == 6 then
				CollectedText.Text = "6/15 Containers"
	
			elseif collected == 7 then
				CollectedText.Text = "7/15 Containers"
	
			elseif collected == 8 then
				CollectedText.Text = "8/15 Containers"
	
			elseif collected == 9 then
				CollectedText.Text = "9/15 Containers"
	
			elseif collected == 10 then
				CollectedText.Text = "10/15 Containers"
	
			elseif collected == 11 then
				CollectedText.Text = "11/15 Containers"
	
			elseif collected == 12 then
				CollectedText.Text = "12/15 Containers"
	
			elseif collected == 13 then 
				CollectedText.Text = "13/15 Containers"
	
			elseif collected == 14 then
				CollectedText.Text = "14/15 Containers"
	
			elseif collected == 15 then
				game.Workspace.Sounds.Reward.Playing = true
				CollectedText.TextColor3 = Color3.new(0, 0.976806, 0)
				CollectedText.Text = "15/15 Containers"
				wait(2)
				CollectedText.Parent:Destroy()
				wait(0.01)
				local HumanoidRootPart = player.Character.HumanoidRootPart
				HumanoidRootPart.CFrame = game.Workspace.End.CFrame
			end
			print("Got it!")
		end)
	end
    repeat wait() until collected == 15
    newContainer:Destroy()
    print("Containers were found")
    startCollecter()
end

startCollecter()
1 Like

okay, i will try it out, thank you

@Daronion it still does not work

I see. Unfortunately just from code I can’t tell how this looks within your studio project. If that’s okay with you and there are no privacy issues, you can either send your roblox studio file here, or DM it to me, and I can get it sorted for you :slight_smile:

1 Like