How do I stop local script from running until it is in player gui?

script.Parent.AncestryChanged:Connect(function(child, parent)
	if parent == game.Players.LocalPlayer.PlayerGui.talking then
		local npc = game.Workspace:FindFirstChild(script.Parent.npcName.Value)--could error because the value might not have changed yet and might be nil
		print("NPC'S NAME IS "..npc.Name)
		local property = npc.Humanoid.Health
		while wait() do
			if property <= 0 then
				game.Players.LocalPlayer.Character.Humanoid.AutoRotate = true
				game.Players.LocalPlayer.Character.HumanoidRootPart.Anchored = false
				game.ReplicatedStorage.changeTalkValue:FireServer(false)
				script.Parent:Destroy()
			end
		end
	end
end)

the above script is a local script, inside server storage
it is supposed to-
check if its ancestry changed
if it has, check if it is in the right place
if it is, then create a variable for the npc’s name (ive checked on client side, the value does seem to change)
then create a variable for its health, then loop constantly checking if the health is 0

but that print statement? it never runs
which makes me assume that when i clone this (which is what im doing) and parent it, it doesnt count as triggering ancestry changed

in which case, how do i stop it from running until its inside the player gui? if i say

while wait() do
if script.Parent.Parent == game.Players.LocalPlayer.PlayerGui.Talking then

wouldnt that error? Because since its in server storage, it doesnt know what the local player is…?

or, does the local script just not run in the server storage, in which case, i dont need any ancestry changed event at all, and itll only run when i put it in a local area?

i mightve phrased a lot of this poorly, if youre confused ill try to answer any questions

So from what I could make of this, (and what I can think of) you could disable the localscript (from properties) and whichever script is managing the cloning process would enable the script once it has been cloned and parented.

I just read the title and there’s multiple ways to do this:

repeat wait() until script.Parent == player.PlayerGui  or script.Parent.Name == "PlayerGui" 
-- if you don't what player you're sending this to

or the 2nd way, which is better and more cost-effective, set the script disabled in Replicatedstorage or wherever you’re storing it, and copy it into the player’s playergui, and set the script.Disable = false (undisabling it).

well since you only read the title heres a bit of info that might change this a little bit-

the script is a child of a frame

so then would i just clone the frame and script separately and then make the script a child of the frame or

Well whatever script you’re using to facilitate it (since you said you weren’t storing it in PlayerGui, there has to be a script that’s moving the frame + the script into the PlayerGui right?), make that script also be the one to undisable it. I.e so here would be the steps ig

  1. Disable the script inside the frame
  2. Move/Clone the ScreenGui/Frame into the PlayerGui
  3. Undisable the script

Local Script doesn’t work in server storage.