How to make the script wait until something destroy

Here’s the script

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
	local ScreenGUI = Player.PlayerGui
	local StarterScreen = ScreenGUI.Starterscreen
	local Team = Player.Team
	
	repeat task.wait() until StarterScreen.nil --Assume this(nil) destroy or destroying
	print("started")
	
		local newHighlight = Instance.new("Highlight")
		newHighlight.DepthMode = Enum.HighlightDepthMode.Occluded
		newHighlight.OutlineColor = Player.TeamColor.Color
		newHighlight.Parent = Character
			
	end)
end)

Ok so what i want to make is for the script to wait until the Starterscreen get deleted then it can continue BUT if i replaced the nil with .destroying the it would already tiggered before the Starterscreen get deleted but if replaced it with :Destroy then it would immediatly destroy the screen gui somehow

The Full Script :

(Client)

local RemoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteFunction")
local Camera = workspace.CurrentCamera
local CameraObject = game.Workspace:WaitForChild("CameraJoin")

local ScreenGUI = script.Parent
local Character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local TeamBlue = script.Parent.Blue --The button
local TeamRed = script.Parent.Red --The Button

Camera.CameraType = Enum.CameraType.Scriptable
Camera.CameraSubject = CameraObject
Camera.CFrame = CameraObject.CFrame


TeamRed.MouseButton1Click:Connect(function()
	
	local Red = Color3.fromRGB(255,0,0)
	
	RemoteEvent:InvokeServer(Red, Character)
	--Camera.CFrame = Camera.CFrame * CFrame.fromOrientation(math.rad(-19),math.rad(85), 0)
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	
	Camera.CFrame = CFrame.fromOrientation(math.rad(-20),math.rad(90), 0)
	
	ScreenGUI:Destroy()
	
end)

TeamBlue.MouseButton1Click:Connect(function()
	
	local Blue = Color3.fromRGB(0,0,255)
	
	RemoteEvent:InvokeServer(Blue, Character)
	
	Camera.CameraType = Enum.CameraType.Custom
	Camera.CameraSubject = Humanoid
	Camera.CFrame = CFrame.fromOrientation(math.rad(-20),math.rad(-90), 0)
	
	ScreenGUI:Destroy()
	
end)

(Server)

local Remote = game:GetService("ReplicatedStorage"):WaitForChild("RemoteFunction")

local RedSpawn = workspace.RedBase.RedSpawn
local BlueSpawn = workspace.BlueBase.BlueSpawn

Remote.OnServerInvoke = function(player, colour, character)
	
	if colour == Color3.fromRGB(255,0,0) then
		local red = game.Teams.Red
		player.Team = red
		
		
		
		character:PivotTo(RedSpawn.CFrame)
    	
	elseif colour == Color3.fromRGB(0,0,255) then
		local blue = game.Teams.Blue
		player.Team = blue
		

		character:PivotTo(BlueSpawn.CFrame)
	
	end
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		
	local ScreenGUI = Player.PlayerGui
	local StarterScreen = ScreenGUI.Starterscreen
	local Team = Player.Team
	
	repeat task.wait() until nil
	print("started")
	
		local newHighlight = Instance.new("Highlight")
		newHighlight.DepthMode = Enum.HighlightDepthMode.Occluded
		newHighlight.OutlineColor = Player.TeamColor.Color
		newHighlight.Parent = Character
			
	end)
end)
2 Likes

You should look into this event:

StarterScreen.Destroying:Wait()
1 Like

or just use .Destroying:Connect(function()

2 Likes

After reading some event, all of them din’t work even .Destroying:Wait and the Starterscreen.Parent == nil. Cause after the deletion of starterscreen, the script doesnt continue

Wait, are you destroying the Gui on client? If yes then server will still have that gui and only the player will not.

(too long didn’t read) i’m gonna assume you have a starterscreen variable at the beginning of the script (im too lazy) and since it’s deleted it’ll yield the entire script

use .Destroying or follow what the person above me said

1 Like

Thank You after a bit of checking, i realise that the script deletion proccess is at client rather than the server

1 Like

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