Script giving error instead of going to else

My script should be checking if a player’s name is within the playerTeam folder which is in the teams folder which is in replicated storage. But when it can’t find the player’s name, it just gives an error instead of going to the else statement.

game.ReplicatedStorage.Remotes.InviteToTeam.OnServerEvent:Connect(function(player, playerTeam, playerToInvite)
	if game.ReplicatedStorage.Teams[playerTeam][playerToInvite] then
		print("Player already in team!")
		if game.Players[playerToInvite] then
			local invitedPlayer ="StringValue")
			invitedPlayer.Parent = game.ReplicatedStorage.Teams[playerTeam]
			invitedPlayer.Name = tostring(playerToInvite)
			invitedPlayer.Value = tostring(playerToInvite)

That depends on how you’re firing the arguments to the event, where are you using this event?

When the player presses the invite button, it fires the InviteToTeam event. Arguments being the inviter’s team and the player they’re inviting

Obviously it won’t go to the else statement. if the code errors, the execution stops (unless it’s wrapped in a pcall). You can use FindFirstChild for indexing, which just returns nil if it can’t find anything so the code won’t error and will actually go to the else statement.

Maybe you could use FindFirstChild as someone else has already mentioned rather than square brackets