Why does the script reset when the player dies?

Hello everyone. For some reason, this script resets whenever I die. That’s basically it.

Here is the LocalScript:

local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
local runService = game:GetService("RunService")

local player = players.LocalPlayer
local camera = workspace.CurrentCamera

local introFocusPoint = workspace.C0YES:WaitForChild("Head").Position
local angle = 0

local mainScreenGui = script.Parent:WaitForChild("MainScreenGui")
local loadingScreenGui = script.Parent:WaitForChild("LoadingScreenGui")

local playButton = mainScreenGui:WaitForChild("PlayButton")

loadingScreenGui.Parent = player:FindFirstChild("PlayerGui")

for i = 1,#"Loading...",1 do
	loadingScreenGui:FindFirstChild("LoadingScreenFrame"):FindFirstChild("LoadingLabel").Text = string.sub("Loading...",1,i)

	local keySound = Instance.new("Sound",workspace)
	keySound.SoundId = "rbxassetid://7450944439"
	keySound.PlaybackSpeed = Random.new():NextNumber(0.95,1)
	keySound.Volume = Random.new():NextNumber(0.4,0.6)
	keySound:Play()

	local waitTime = Random.new():NextNumber(0.1,0.4)
	wait(waitTime)
end

wait(2)

camera.CameraType = Enum.CameraType.Scriptable
camera.Focus = CFrame.new(introFocusPoint)

playButton.Visible = true

player:RequestStreamAroundAsync(introFocusPoint)

loadingScreenGui:Destroy()

lighting:FindFirstChild("Blur").Enabled = true
runService:BindToRenderStep("rotateCamera",Enum.RenderPriority.Camera.Value,function()
	local cameraPosition = introFocusPoint + Vector3.new(50 * math.cos(angle),20,50 * math.sin(angle))
	camera.CoordinateFrame = CFrame.new(cameraPosition,introFocusPoint)
	angle = angle + math.rad(0.65)
end)

local function stopIntroScreen()
	lighting:FindFirstChild("Blur").Enabled = false
	playButton.Visible = false
	
	runService:UnbindFromRenderStep("rotateCamera")
	camera.CameraType = Enum.CameraType.Custom
end

playButton.MouseButton1Down:Connect(stopIntroScreen)

Try moving the LocalScript to StarterPlayerScripts. Here should be an edited version of the code:

local players = game:GetService("Players")
local lighting = game:GetService("Lighting")
local runService = game:GetService("RunService")

local player = players.LocalPlayer
local camera = workspace.CurrentCamera

local introFocusPoint = workspace.C0YES:WaitForChild("Head").Position
local angle = 0

local mainScreenGui = player.PlayerGui:WaitForChild("MainScreenGui")
local loadingScreenGui = player.PlayerGui:WaitForChild("LoadingScreenGui")

local playButton = mainScreenGui:WaitForChild("PlayButton")

loadingScreenGui.Parent = player:FindFirstChild("PlayerGui")

for i = 1,#"Loading...",1 do
	loadingScreenGui:FindFirstChild("LoadingScreenFrame"):FindFirstChild("LoadingLabel").Text = string.sub("Loading...",1,i)

	local keySound = Instance.new("Sound",workspace)
	keySound.SoundId = "rbxassetid://7450944439"
	keySound.PlaybackSpeed = Random.new():NextNumber(0.95,1)
	keySound.Volume = Random.new():NextNumber(0.4,0.6)
	keySound:Play()

	local waitTime = Random.new():NextNumber(0.1,0.4)
	wait(waitTime)
end

task.wait(2)

camera.CameraType = Enum.CameraType.Scriptable
camera.Focus = CFrame.new(introFocusPoint)

playButton.Visible = true

player:RequestStreamAroundAsync(introFocusPoint)

loadingScreenGui:Destroy()

lighting:FindFirstChild("Blur").Enabled = true
runService:BindToRenderStep("rotateCamera",Enum.RenderPriority.Camera.Value,function()
	local cameraPosition = introFocusPoint + Vector3.new(50 * math.cos(angle),20,50 * math.sin(angle))
	camera.CoordinateFrame = CFrame.new(cameraPosition,introFocusPoint)
	angle = angle + math.rad(0.65)
end)

local function stopIntroScreen()
	lighting:FindFirstChild("Blur").Enabled = false
	playButton.Visible = false
	
	runService:UnbindFromRenderStep("rotateCamera")
	camera.CameraType = Enum.CameraType.Custom
end

playButton.MouseButton1Down:Connect(stopIntroScreen)

I do think it made it better, but the loadingScreenGui gets Undestroyed somehow.

Try turning off ResetOnSpawn on the ScreenGui

1 Like

Oh, my bad, I forgot to mention something. For both MainScreenGui and LoadingScreenGui, turn “ResetOnSpawn” off in properties.

image

I’m not entierly sure if this is the solution, but I think it is.

1 Like

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