How Do I Refer to Gui Without Placing Gui in Script?

So, as my question stated, how do I change this script to refer to the Gui without having to place the Gui in the Script to run?

function died (playerDied, random)
	playerDied.Humanoid.Died:Connect(function()
		script.ScreenGui:Clone().Parent = random.PlayerGui
		local player = game:GetService("Players")
		player.CharacterAutoLoads = false
	end)
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	died(player.Character,player)
	player.CharacterAdded:Connect(function(dead)
		died(dead,player)
	end)
end)

I’m not entirely sure what you mean…
Do you mean something like this?

game.Players.PlayerName.PlayerGui.ScreenGui.GuiObject

Or this?

game.ReplicatedStorage.GuiObject

Or something else?

Yeah, the script is in ServerScriptService while the Gui is in StarterPlayer so I need the Gui in the StarterPlayer to show up since this script requires the Gui to be in the script.

Inside of your PlayerAdded, you should be able to do this:

player.PlayerGui.ScreenGui.GuiObject

This player.CharacterAdded function will never run the first time, because you’re yielding for your CharacterAdded:Wait() Event first, before detecting an added Character the next time

There’s a lot to change with this script here, but I won’t get into all the main details

If you wanna refer to the Gui, add more variables to your script & consider moving your script somewhere else, like ReplicatedStorage :L

local RS = game:GetService("ReplicatedStorage")
local Plrs = game:GetService("Players")
local ScreenGui = RS:WaitForChild("ScreenGui")

local function PlayerDied(Plr)
    local PlrGui = Plr:WaitForChild("PlayerGui")

    local UIClone = ScreenGui:Clone()
    UIClone.Parent = PlrGui

    Plrs.CharacterAutoLoads = false -- Keep in mind that this will toggle for EVERY player in the server
end

local function PlayerAdded(Plr)
    Plr.CharacterAdded:Connect(function(Char)
        local Hum = Char:WaitForChild("Humanoid")

        Hum.Died:Connect(function()
            PlayerDied(Plr)
        end)
    end)
end

Plrs.PlayerAdded:Connect(PlayerAdded)

Btw, when I place that in place of the script, it underlines the last couple lines and fives an error.

What is the error?
character limit

Can you try again? I forgot to add a couple more things there, mb

So I put it in again and it underlines RS for some reason.

How is it underlining though? Did you copy & paste the entire script?

You didn’t add the RS line here:

Alright, that seemed to work, but it won’t run because this script is not working.

local Market = game:GetService("MarketplaceService")

Market.ProcessReciept = function(reciept)
	if reciept.ProductId == 1306306067 then
		local player = game.Players:GetPlayerByUserId(reciept.PlayerId)
		player:LoadCharacter()
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

The whole idea is to give the player a Gui when they die so that they can pay to respawn at the last checkpoint or they just can reset to the start.