How can I make this script reset properly on death?

Hey there, Allow me to introduce myself. I’m x5tb and I have a problem with my script, I’ve been working on a simple menu and everything was going well until I hit a snag. The problem is that the game is not replayable and if you die enough times the gui stops working, Here’s some video of what happens:
https://gyazo.com/4a174656f62d46894822970045070fe1

The goal of the script is to operate a menu. The idea is that when a player joins they are placed in a box far away, Their camera is moved to a location (specifically a brick) and blurred. Then UI will be onscreen and there is one button AKA “Levels” This is the button to access most of the levels that I will be adding.

Once the button is clicked a box with leves 1 to ??? will popup, There is a X located on the top left to close and when you click a level, Example: Level one, The player’s camera is unblurred they get control again and their humanoidrootpart is teleported to the level they selected.

This is all ran by 1 local script located In StarterPlayer > StarterPlayerScripts > “MenuManager”

--MAIN VARIABLES--
local cam = workspace.CurrentCamera
local Location = game.Workspace.MenuCameraPART
local player = game.Players.LocalPlayer

--GUI VARIABLES--
local Levels = player.PlayerGui:WaitForChild("Menu").Levels
local Close = player.PlayerGui.Menu.Frame.Close
local Menu = player.PlayerGui.Menu.Frame
local Level1 = player.PlayerGui.Menu.Frame.Level1Button

--OTHER VARIABLES--
local L1Start = game.Workspace.Level1.StartPart
local Blur = Instance.new("BlurEffect")

--MAIN MENU HANDLER--
repeat wait() until cam.CameraSubject ~= nil
local function Main()
	cam.CameraType = Enum.CameraType.Scriptable

	cam.CFrame = Location.CFrame
	
	--BLUR HANDLER--
	Blur.Parent = game.Lighting
	Blur.Size = 15
	Blur.Enabled = true
	Blur.Name = "MenuBlur"
	
	--UI SETUP--
	Levels.Visible = true
	
	--UI CLICK FUNCTIONS--
	Levels.MouseButton1Click:Connect(function()
		Menu.Visible = true
	end)

	Close.MouseButton1Click:Connect(function()
		Menu.Visible = false
	end)
	
end
Main()

--LEVELS--
Level1.MouseButton1Click:Connect(function()
	Blur.Parent = game.ReplicatedStorage
	Menu.Visible = false
	Levels.Visible = false
	player.Character.HumanoidRootPart.CFrame = L1Start.CFrame
	cam.CameraType = Enum.CameraType.Custom
	cam.CameraSubject = player.Character.Humanoid
end)

--PLAYER DEATH RESET--

player.Character.Humanoid.Died:Connect(function()
	print("Reseting...")
	wait(1)
	Main()
	print("Succesfully reset!")
end)

--LEVEL1--
local Barrier1 = game.Workspace.Level1.StartBarrier.Barrier
local Barrier2 = game.Workspace.Level1.EndBarrier.Barrier
local debounce = false
local delayN = true

Barrier1.Touched:Connect(function(player)
	if not debounce then
		debounce = true
		
		
		repeat wait() until delayN == false
		debounce = false
	end
end)

And now the problem I shall talk about the problem at hand, The script is playable twice using a function when the player dies. But for some reason doesn’t work Infinitely, I’m asking for help on this script or help to do this a better way. Any suggestions, things I can improve, ways I can organize it better, I’m really open to anything at this point. Thank you for taking your time to read this.

Thanks for any help you can provide with my script! Cheers!

Put it inside of StarterCharacterScripts so it runs everytime the character spawns.

Or just do player.CharacterAdded instead of Humanoid.Died since it takes 5 seconds to respawn and that might mess up the script

2 Likes

Hey @legs_v! Thanks for your response. player.CharacterAdded Is working perfectly, I didn’t know it was that simple, All I had to do was change 1 line of code and remove another and, BAM!

Thanks again!