Stages and rebirth

Hello, im newbie in lua, and i have litlle problem

The script for buying the next stages works until I rebirth, after rebirth the loop the script stops working. I tried to act with events and various things that I can do at the moment. But I didn’t do anything

After restarting the test /game, it starts working again. Until another rebirth

Stage Script (Local Scirpt)

local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")



local stagesFolder = game.Workspace:WaitForChild("Stages")
local loadEvent = RS:WaitForChild("Load")
local loadEvent2 = RS:WaitForChild("Load2")
local buyStageEvent = RS:WaitForChild("BuyStage")
local buStage = RS:WaitForChild("BuyStageEvent")
local rebStageEvent2 = RS:WaitForChild("RebirthStage2")



local function si()
	local loaded = false
loadEvent2.OnClientEvent:Connect(function()
	loaded = true
end)

repeat wait() until loaded

local plr = Players.LocalPlayer
local stageValue = plr:WaitForChild("Stages")

for i, stage in pairs(stagesFolder:GetChildren()) do
	if tonumber(stage.Name) <= stageValue.Value then
		stage:Destroy()
	else
		stage:WaitForChild("BuyPart"):WaitForChild("ProximityPrompt").Triggered:Connect(function()
			local result = buyStageEvent:InvokeServer(stage)
			if result == "Success" then
				stage:Destroy()
			else
				warn("To low Spirits")
				local gui = script.Parent.Parent.PlayerGui.stages
				gui.Enabled = true
				end
				
			end)

		end
		
	end
	
end

loadEvent.OnClientEvent:Connect(si)

Stage Script 2 (Script)

local RS = game:GetService("ReplicatedStorage")


local buyStage = RS:WaitForChild("BuyStage")
local buyEvnet = RS:WaitForChild("BuyStageEvent")


local function buyBarrier(plr, stage)
	if plr.leaderstats.Spirit.Value >= stage.Price.Value then
		plr.leaderstats.Spirit.Value -= stage.Price.Value
		plr.Stages.Value += 1
		stage:Destroy()		
		return "Success"
	else
		return "Error"
	end
end

buyStage.OnServerInvoke = buyBarrier

rebirth script (script)

local RS = game:GetService("ReplicatedStorage")
local RSStages = RS:WaitForChild("Stages")
local copyStages = RSStages:Clone()
local stages = workspace.Stages

local rebStageEvent = RS:WaitForChild("RebirthStage")



game.ReplicatedStorage.Rebirth.OnServerEvent:connect(function(player)
	if player.leaderstats.Spirit.Value >= 5000 * player.leaderstats.Rebirth.Value then
		stages:Destroy()
		player.leaderstats.Energy.Value = 0
		player.leaderstats.Spirit.Value = 0
		player.Stages.Value = 0
		player.leaderstats.Rebirth.Value += 1
		game.ReplicatedStorage.Rebirth:FireClient(player)
		player:LoadCharacter(player)
		rebStageEvent:FireClient(player)
		copyStages.Parent = workspace
	end
end)

Anyone have any idea how to solve this?

For the Stage Script 2 shouldn’t it be

buyStage.OnServerInvoke:Connect(buyBarrier)

Also, for the rebirth script you are only cloning the stage once. Which might be the problem.

I’ve tried all kinds of things. I was able to fix it easily with transparency and cancolide. But thanks anyway for your willingness to help :smiley: